diff options
Diffstat (limited to 'src/init.cpp')
-rw-r--r-- | src/init.cpp | 80 |
1 files changed, 34 insertions, 46 deletions
diff --git a/src/init.cpp b/src/init.cpp index daf2b5a6d..969263191 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -23,11 +23,12 @@ #include <sys/wait.h> #include <sys/stat.h> -#include <errno.h> -#include <sys/types.h> -#include <pwd.h> +#include <cerrno> #include <grp.h> +#include <pwd.h> +#include <sys/types.h> #endif +#include <thread> Anope::string Anope::ConfigDir = "conf", Anope::DataDir = "data", Anope::ModuleDir = "lib", Anope::LocaleDir = "locale", Anope::LogDir = "logs"; @@ -57,7 +58,7 @@ static void ParseCommandLineArguments(int ac, char **av) if (option.empty()) continue; - CommandLineArguments.push_back(std::make_pair(option, param)); + CommandLineArguments.emplace_back(option, param); } } @@ -71,11 +72,11 @@ static bool GetCommandLineArgument(const Anope::string &name, char shortname, An { param.clear(); - for (std::vector<std::pair<Anope::string, Anope::string> >::iterator it = CommandLineArguments.begin(), it_end = CommandLineArguments.end(); it != it_end; ++it) + for (const auto &[argument, value] : CommandLineArguments) { - if (it->first.equals_ci(name) || (it->first.length() == 1 && it->first[0] == shortname)) + if (argument.equals_ci(name) || (argument.length() == 1 && argument[0] == shortname)) { - param = it->second; + param = value; return true; } } @@ -131,7 +132,7 @@ void Anope::HandleSignal() try { - Configuration::Conf *new_config = new Configuration::Conf(); + auto *new_config = new Configuration::Conf(); Configuration::Conf *old = Config; Config = new_config; Config->Post(old); @@ -217,19 +218,16 @@ static void remove_pidfile() static void write_pidfile() { - FILE *pidfile = fopen(Config->GetBlock("serverinfo")->Get<const Anope::string>("pid").c_str(), "w"); - if (pidfile) - { + const auto pidfile = Config->GetBlock("serverinfo")->Get<const Anope::string>("pid"); + std::ofstream stream(pidfile.str()); + if (!stream.is_open()) + throw CoreException("Can not write to PID file " + pidfile); #ifdef _WIN32 - fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId())); + stream << GetCurrentProcessId() << std::endl; #else - fprintf(pidfile, "%d\n", static_cast<int>(getpid())); + stream << getpid() << std::endl; #endif - fclose(pidfile); - atexit(remove_pidfile); - } - else - throw CoreException("Can not write to PID file " + Config->GetBlock("serverinfo")->Get<const Anope::string>("pid")); + atexit(remove_pidfile); } static void setuidgid() @@ -258,14 +256,10 @@ static void setuidgid() gid = g->gr_gid; } - for (unsigned i = 0; i < Config->LogInfos.size(); ++i) + for (const auto &li : Config->LogInfos) { - LogInfo& li = Config->LogInfos[i]; - - for (unsigned j = 0; j < li.logfiles.size(); ++j) + for (const auto *lf : li.logfiles) { - LogFile* lf = li.logfiles[j]; - errno = 0; if (chown(lf->filename.c_str(), uid, gid) != 0) Log() << "Unable to change the ownership of " << lf->filename << " to " << uid << "/" << gid << ": " << Anope::LastError(); @@ -289,7 +283,7 @@ static void setuidgid() #endif } -void Anope::Init(int ac, char **av) +bool Anope::Init(int ac, char **av) { /* Set file creation mask and group ID. */ #if defined(DEFUMASK) && HAVE_UMASK @@ -304,7 +298,8 @@ void Anope::Init(int ac, char **av) if (GetCommandLineArgument("version", 'v')) { Log(LOG_TERMINAL) << "Anope-" << Anope::Version() << " -- " << Anope::VersionBuildString(); - throw CoreException(); + Anope::ReturnValue = EXIT_SUCCESS; + return false; } if (GetCommandLineArgument("help", 'h')) @@ -330,7 +325,8 @@ void Anope::Init(int ac, char **av) Log(LOG_TERMINAL) << ""; Log(LOG_TERMINAL) << "Further support is available from https://www.anope.org/"; Log(LOG_TERMINAL) << "Or visit us on IRC at irc.anope.org #anope"; - throw CoreException(); + Anope::ReturnValue = EXIT_SUCCESS; + return false; } if (GetCommandLineArgument("nofork", 'n')) @@ -411,7 +407,7 @@ void Anope::Init(int ac, char **av) Anope::LogDir = arg; } - /* Chdir to Services data directory. */ + /* Chdir to Anope data directory. */ if (chdir(Anope::ServicesDir.c_str()) < 0) { throw CoreException("Unable to chdir to " + Anope::ServicesDir + ": " + Anope::LastError()); @@ -420,11 +416,6 @@ void Anope::Init(int ac, char **av) Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::VersionBuildString(); #ifdef _WIN32 - if (!SupportedWindowsVersion()) - throw CoreException(GetWindowsVersion() + " is not a supported version of Windows"); -#endif - -#ifdef _WIN32 Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "\\" << ServicesConf.GetName(); #else Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "/" << ServicesConf.GetName(); @@ -453,7 +444,7 @@ void Anope::Init(int ac, char **av) sigemptyset(&mask); sigsuspend(&mask); - exit(Anope::ReturnValue); + return false; } else if (i == -1) { @@ -479,7 +470,7 @@ void Anope::Init(int ac, char **av) catch (const ConfigException &ex) { Log(LOG_TERMINAL) << ex.GetReason(); - Log(LOG_TERMINAL) << "*** Support resources: Read through the services.conf self-contained"; + Log(LOG_TERMINAL) << "*** Support resources: Read through the anope.conf self-contained"; Log(LOG_TERMINAL) << "*** documentation. Read the documentation files found in the 'docs'"; Log(LOG_TERMINAL) << "*** folder. Visit our portal located at https://www.anope.org/. Join"; Log(LOG_TERMINAL) << "*** our support channel on /server irc.anope.org channel #anope."; @@ -489,9 +480,9 @@ void Anope::Init(int ac, char **av) /* Create me */ Configuration::Block *block = Config->GetBlock("serverinfo"); Me = new Server(NULL, block->Get<const Anope::string>("name"), 0, block->Get<const Anope::string>("description"), block->Get<const Anope::string>("id")); - for (botinfo_map::const_iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it) + for (const auto &[_, bi] : *BotListByNick) { - it->second->server = Me; + bi->server = Me; ++Me->users; } @@ -503,10 +494,6 @@ void Anope::Init(int ac, char **av) /* Initialize multi-language support */ Language::InitLanguages(); - /* Initialize random number generator */ - block = Config->GetBlock("options"); - srand(block->Get<unsigned>("seed") ^ time(NULL)); - /* load modules */ Log() << "Loading modules..."; for (int i = 0; i < Config->CountBlock("module"); ++i) @@ -523,7 +510,7 @@ void Anope::Init(int ac, char **av) std::cerr << "WARNING: You are currently running Anope as the root superuser. Anope does not" << std::endl; std::cerr << " require root privileges to run, and it is discouraged that you run Anope" << std::endl; std::cerr << " as the root superuser." << std::endl; - sleep(3); + std::this_thread::sleep_for(std::chrono::seconds(3)); } } @@ -547,8 +534,8 @@ void Anope::Init(int ac, char **av) Anope::string sid = IRCD->SID_Retrieve(); if (Me->GetSID() == Me->GetName()) Me->SetSID(sid); - for (botinfo_map::iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it) - it->second->GenerateUID(); + for (const auto &[_, bi] : *BotListByNick) + bi->GenerateUID(); } /* Load up databases */ @@ -560,8 +547,9 @@ void Anope::Init(int ac, char **av) FOREACH_MOD(OnPostInit, ()); - for (channel_map::const_iterator it = ChannelList.begin(), it_end = ChannelList.end(); it != it_end; ++it) - it->second->Sync(); + for (const auto &[_, ci] : ChannelList) + ci->Sync(); Serialize::CheckTypes(); + return true; } |