From: Remi Gacogne Date: Tue, 1 Aug 2023 14:20:50 +0000 (+0200) Subject: ixfrdist: Reduce the complexity of the main function X-Git-Tag: rec-5.0.0-alpha1~62^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48d1f24b87dfd95696c0db8311c81dc622717102;p=thirdparty%2Fpdns.git ixfrdist: Reduce the complexity of the main function --- diff --git a/pdns/ixfrdist.cc b/pdns/ixfrdist.cc index d4d319927d..08528e5082 100644 --- a/pdns/ixfrdist.cc +++ b/pdns/ixfrdist.cc @@ -1179,12 +1179,26 @@ static bool parseAndCheckConfig(const string& configpath, YAML::Node& config) { return retval; } -int main(int argc, char** argv) { - g_log.setLoglevel(Logger::Notice); - g_log.toConsole(Logger::Notice); - g_log.setPrefixed(true); - g_log.disableSyslog(true); - g_log.setTimestamps(false); +struct IXFRDistConfiguration +{ + set listeningSockets; + NetmaskGroup wsACL; + ComboAddress wsAddr; + std::string wsLogLevel{"normal"}; + std::string workDir; + uint32_t axfrMaxRecords{0}; + uint16_t keep{0}; + uint16_t axfrTimeout{0}; + uint16_t failedSOARetry{0}; + uint16_t tcpInThreads{0}; + uid_t uid{0}; + gid_t gid{0}; + bool done{false}; +}; + +static std::optional parseConfiguration(int argc, char** argv, FDMultiplexer& fdm) +{ + IXFRDistConfiguration configuration; po::variables_map g_vm; std::string configPath; @@ -1203,23 +1217,23 @@ int main(int argc, char** argv) { if (g_vm.count("help") > 0) { usage(desc); - return EXIT_SUCCESS; + return configuration; } if (g_vm.count("version") > 0) { cout<<"ixfrdist "<(); } catch (const po::error &e) { g_log< s; s.insert(domain["master"].as()); g_domainConfigs[domain["domain"].as()].masters = s; - if (domain["max-soa-refresh"]) { + if (domain["max-soa-refresh"].IsDefined()) { g_domainConfigs[domain["domain"].as()].maxSOARefresh = domain["max-soa-refresh"].as(); } g_stats.registerDomain(domain["domain"].as()); @@ -1276,20 +1290,13 @@ int main(int argc, char** argv) { g_log<(); if (g_compress) { g_log< allSockets; for (const auto& addr : config["listen"].as>()) { for (const auto& stype : {SOCK_DGRAM, SOCK_STREAM}) { try { @@ -1300,8 +1307,8 @@ int main(int argc, char** argv) { if (stype == SOCK_STREAM) { SListen(s, 30); // TODO make this configurable } - fdm->addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest); - allSockets.insert(s); + fdm.addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest); + configuration.listeningSockets.insert(s); } catch (const runtime_error& exp) { g_log<(); - if (!(newgid = atoi(gid.c_str()))) { - struct group *gr = getgrnam(gid.c_str()); + if (config["gid"].IsDefined()) { + auto gid = config["gid"].as(); + try { + configuration.gid = pdns::checked_stoi(gid); + } + catch (const std::exception& e) { + g_log<gr_gid; + configuration.gid = gr->gr_gid; } } - g_log<(); + try { - wsACL.addMask("127.0.0.0/8"); - wsACL.addMask("::1/128"); + configuration.wsACL.addMask("127.0.0.0/8"); + configuration.wsACL.addMask("::1/128"); - if (config["webserver-acl"]) { - wsACL.clear(); + if (config["webserver-acl"].IsDefined()) { + configuration.wsACL.clear(); for (const auto &acl : config["webserver-acl"].as>()) { - wsACL.addMask(acl); + configuration.wsACL.addMask(acl); } } } @@ -1354,95 +1363,153 @@ int main(int argc, char** argv) { had_error = true; } - string loglevel = "normal"; if (config["webserver-loglevel"]) { - loglevel = config["webserver-loglevel"].as(); + configuration.wsLogLevel = config["webserver-loglevel"].as(); } + } - // Launch the webserver! + if (config["uid"].IsDefined()) { + string uid = config["uid"].as(); try { - std::thread(&IXFRDistWebServer::go, IXFRDistWebServer(config["webserver-address"].as(), wsACL, loglevel)).detach(); + configuration.uid = pdns::checked_stoi(uid); } - catch (const std::exception& exp) { - g_log<(); - if (!(newuid = atoi(uid.c_str()))) { - struct passwd *pw = getpwnam(uid.c_str()); + if (configuration.uid != 0) { + //NOLINTNEXTLINE(concurrency-mt-unsafe): only one thread at this point + const struct passwd *pw = getpwnam(uid.c_str()); if (pw == nullptr) { g_log<pw_uid; + configuration.uid = pw->pw_uid; } } + } + + configuration.workDir = config["work-dir"].as(); + configuration.keep = config["keep"].as(); + configuration.axfrTimeout = config["axfr-timeout"].as(); + configuration.failedSOARetry = config["failed-soa-retry"].as(); + configuration.axfrMaxRecords = config["axfr-max-records"].as(); + configuration.tcpInThreads = config["tcp-in-threads"].as(); + + if (had_error) { + return std::nullopt; + } + return configuration; + } + catch (const YAML::Exception& exp) { + had_error = true; + g_log<(FDMultiplexer::getMultiplexerSilent()); + if (!fdm) { + g_log<done) { + return EXIT_SUCCESS; + } + + bool had_error = false; + try { + if (configuration->gid != 0) { + g_log<gid<gid) < 0) { + g_log<gid<<": "<wsAddr, configuration->wsACL, configuration->wsLogLevel)).detach(); + } + catch (const std::exception& exp) { + g_log<uid != 0) { + g_log<uid<uid) < 0) { + g_log<uid<<": "<uid); if (pw == nullptr) { if (setgroups(0, nullptr) < 0) { g_log<pw_name, newgid) < 0) { + if (initgroups(pw->pw_name, configuration->gid) < 0) { g_log<(), - config["keep"].as(), - config["axfr-timeout"].as(), - config["failed-soa-retry"].as(), - config["axfr-max-records"].as()); + configuration->workDir, + configuration->keep, + configuration->axfrTimeout, + configuration->failedSOARetry, + configuration->axfrMaxRecords); vector tcpHandlers; - tcpHandlers.reserve(config["tcp-in-threads"].as()); + tcpHandlers.reserve(configuration->tcpInThreads); for (size_t i = 0; i < tcpHandlers.capacity(); ++i) { tcpHandlers.push_back(std::thread(tcpWorker, i)); } struct timeval now; - for(;;) { + for (;;) { gettimeofday(&now, 0); fdm->run(&now); if (g_exiting) { g_log<listeningSockets) { try { closesocket(fd); - } catch(PDNSException &e) { + } catch (const PDNSException &e) { g_log<