Simplify the codes that InspIRCd can exit with.

The custom codes that InspIRCd exits with are not very useful and can confuse init systems like systemd which assume that certain
exit codes mean certain things. INSPIRCD_BINARY_EXIT was a workaround
for this in v3 but considering thatsers have to check the logs anyway
so we may as well just use EXIT_SUCCESS and EXIT_FAILURE.
This commit is contained in:
Sadie Powell 2023-07-13 13:31:06 +01:00
parent d91d707c6d
commit b4e3e97329
11 changed files with 33 additions and 138 deletions

View File

@ -1,42 +0,0 @@
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2013, 2019 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
* Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
* Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
* Copyright (C) 2006-2008 Craig Edwards <brain@inspircd.org>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/** Valid exit codes to be used with InspIRCd::Exit()
*/
enum ExitStatus
{
EXIT_STATUS_NOERROR = 0, /* No error */
EXIT_STATUS_DIE = 1, /* Operator issued DIE */
EXIT_STATUS_CONFIG = 2, /* Config error */
EXIT_STATUS_LOG = 3, /* Log file error */
EXIT_STATUS_FORK = 4, /* fork() failed */
EXIT_STATUS_ARGV = 5, /* Invalid program arguments */
EXIT_STATUS_PID = 6, /* Couldn't write PID file */
EXIT_STATUS_SOCKETENGINE = 7, /* Couldn't start socket engine */
EXIT_STATUS_ROOT = 8, /* Refusing to start as root */
EXIT_STATUS_MODULE = 9, /* Couldn't load a required module */
EXIT_STATUS_SIGTERM = 10 /* Received SIGTERM */
};

View File

@ -56,52 +56,6 @@ Allow the server to start as root (not recommended).
.br
Displays the InspIRCd version and exits.
.SH "EXIT STATUS"
.TP
.B "0 (EXIT_STATUS_NOERROR)"
.br
The server exited cleanly.
.TP
.B "1 (EXIT_STATUS_DIE)"
.br
The server exited because the DIE command was executed.
.TP
.B "2 (EXIT_STATUS_CONFIG)"
.br
The server exited because of a configuration file error.
.TP
.B "3 (EXIT_STATUS_LOG)"
.br
The server exited because of a log file error.
.TP
.B "4 (EXIT_STATUS_FORK)"
.br
The server exited because it was unable to fork into the background.
.TP
.B "5 (EXIT_STATUS_ARGV)"
.br
The server exited because an invalid argument was passed to it on the command line.
.TP
.B "6 (EXIT_STATUS_PID)"
.br
The server exited because it was unable to write to the PID file.
.TP
.B "7 (EXIT_STATUS_SOCKETENGINE)"
.br
The server exited because it was unable to initialize the @SOCKETENGINE@ socket engine.
.TP
.B "8 (EXIT_STATUS_ROOT)"
.br
The server exited because the user tried to start as root without \fI--runasroot\fR.
.TP
.B "9 (EXIT_STATUS_MODULE)"
.br
The server exited because it was unable to load a module on first run.
.TP
.B "10 (EXIT_STATUS_SIGTERM)"
.br
The server exited because it received SIGTERM.
.SH "SUPPORT"
IRC support for InspIRCd can be found at ircs://irc.inspircd.org/inspircd.

View File

@ -37,7 +37,6 @@
#include "inspircd.h"
#include "configparser.h"
#include "exitcodes.h"
ServerConfig::ReadResult::ReadResult(const std::string& c, const std::string& e)
: contents(c)
@ -528,7 +527,7 @@ void ServerConfig::Apply(ServerConfig* old, const std::string& useruid)
{
if (!valid)
{
ServerInstance->Exit(EXIT_STATUS_CONFIG);
ServerInstance->Exit(EXIT_FAILURE);
}
return;

View File

@ -27,8 +27,6 @@
#include "inspircd.h"
#include "clientprotocolmsg.h"
#include "exitcodes.h"
#include "core_oper.h"
@ -65,7 +63,7 @@ CmdResult CommandDie::Handle(User* user, const Params& parameters)
const std::string diebuf = "*** DIE command from " + user->GetMask() + ". Terminating.";
ServerInstance->Logs.Error(MODNAME, diebuf);
DieRestart::SendError(diebuf);
ServerInstance->Exit(EXIT_STATUS_DIE);
ServerInstance->Exit(EXIT_FAILURE);
}
else
{

View File

@ -40,7 +40,6 @@
#include <lyra/lyra.hpp>
#include "inspircd.h"
#include "exitcodes.h"
#include "xline.h"
#ifndef _WIN32
@ -92,7 +91,7 @@ namespace
{
fmt::println("If you are sure that you need to run as root then you can pass the {}", fmt::styled("--runasroot", fmt::emphasis::bold));
fmt::println("option to disable this error.");
ServerInstance->Exit(EXIT_STATUS_ROOT);
ServerInstance->Exit(EXIT_FAILURE);
}
#endif
}
@ -151,20 +150,20 @@ namespace
if (setgroups(0, nullptr) == -1)
{
ServerInstance->Logs.Error("STARTUP", "setgroups() failed (wtf?): {}", strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_CONFIG);
InspIRCd::QuickExit(EXIT_FAILURE);
}
struct group* g = getgrnam(SetGroup.c_str());
if (!g)
{
ServerInstance->Logs.Error("STARTUP", "getgrnam({}) failed (wrong group?): {}", SetGroup, strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_CONFIG);
InspIRCd::QuickExit(EXIT_FAILURE);
}
if (setgid(g->gr_gid) == -1)
{
ServerInstance->Logs.Error("STARTUP", "setgid({}) failed (wrong group?): {}", g->gr_gid, strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_CONFIG);
InspIRCd::QuickExit(EXIT_FAILURE);
}
}
@ -176,13 +175,13 @@ namespace
if (!u)
{
ServerInstance->Logs.Error("STARTUP", "getpwnam({}) failed (wrong user?): {}", SetUser, strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_CONFIG);
InspIRCd::QuickExit(EXIT_FAILURE);
}
if (setuid(u->pw_uid) == -1)
{
ServerInstance->Logs.Error("STARTUP", "setuid({}) failed (wrong user?): {}", u->pw_uid, strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_CONFIG);
InspIRCd::QuickExit(EXIT_FAILURE);
}
}
#endif
@ -228,7 +227,7 @@ namespace
{
#ifndef _WIN32
// We use VoidSignalHandler whilst forking to avoid breaking daemon scripts
// if the parent process exits with SIGTERM (15) instead of EXIT_STATUS_NOERROR (0).
// if the parent process exits with SIGTERM (15) instead of EXIT_SUCCESS (0).
signal(SIGTERM, VoidSignalHandler);
errno = 0;
@ -237,7 +236,7 @@ namespace
{
ServerInstance->Logs.Error("STARTUP", "fork() failed: {}", strerror(errno));
fmt::println("{} unable to fork into background: {}", fmt::styled("Error:", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), strerror(errno));
ServerInstance->Exit(EXIT_STATUS_FORK);
ServerInstance->Exit(EXIT_FAILURE);
}
else if (childpid > 0)
{
@ -247,7 +246,7 @@ namespace
// happened and the parent should exit.
while (kill(childpid, 0) != -1)
sleep(1);
InspIRCd::QuickExit(EXIT_STATUS_NOERROR);
InspIRCd::QuickExit(EXIT_SUCCESS);
}
else
{
@ -318,19 +317,19 @@ namespace
if (!result)
{
fmt::println(stderr, "{} {}", fmt::styled("Error:", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), result.message());
ServerInstance->Exit(EXIT_STATUS_ARGV);
ServerInstance->Exit(EXIT_FAILURE);
}
if (do_help)
{
std::cout << cli << std::endl;
ServerInstance->Exit(EXIT_STATUS_NOERROR);
ServerInstance->Exit(EXIT_SUCCESS);
}
if (do_version)
{
fmt::println(INSPIRCD_VERSION);
ServerInstance->Exit(EXIT_STATUS_NOERROR);
ServerInstance->Exit(EXIT_SUCCESS);
}
// Store the relevant parsed arguments
@ -398,7 +397,7 @@ namespace
// Required for returning the proper value of EXIT_SUCCESS for the parent process.
void VoidSignalHandler(int)
{
InspIRCd::QuickExit(EXIT_STATUS_NOERROR);
InspIRCd::QuickExit(EXIT_SUCCESS);
}
}
@ -457,7 +456,7 @@ void InspIRCd::WritePID()
{
fmt::println("Failed to write PID-file '{}', exiting.", pidfile);
this->Logs.Error("STARTUP", "Failed to write PID-file '{}', exiting.", pidfile);
Exit(EXIT_STATUS_PID);
Exit(EXIT_FAILURE);
}
}
@ -503,7 +502,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
this->Logs.Error("STARTUP", "Unable to open config file {}", ConfigFileName);
fmt::println("ERROR: Cannot open config file: {}", ConfigFileName);
fmt::println("Exiting...");
Exit(EXIT_STATUS_CONFIG);
Exit(EXIT_FAILURE);
}
SetSignals();
@ -529,7 +528,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
{
fmt::println("ERROR: Cannot open log files: {}", ex.GetReason());
fmt::println("Exiting...");
Exit(EXIT_STATUS_LOG);
Exit(EXIT_FAILURE);
}
// If we don't have a SID, generate one based on the server name and the server description
@ -560,7 +559,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
{
fmt::println("ERROR: Cannot open log files: {}", ex.GetReason());
fmt::println("Exiting...");
Exit(EXIT_STATUS_LOG);
Exit(EXIT_FAILURE);
}
fmt::println("InspIRCd is now running as '{}'[{}] with {} max open sockets",

View File

@ -28,7 +28,6 @@
#include "inspircd.h"
#include "dynamic.h"
#include "exitcodes.h"
bool ModuleManager::Load(const std::string& modname, bool defer)
{
@ -150,14 +149,14 @@ void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicem
fmt::println("");
fmt::println("[{}] {}", fmt::styled("*", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), LastError());
fmt::println("");
ServerInstance->Exit(EXIT_STATUS_MODULE);
ServerInstance->Exit(EXIT_FAILURE);
}
}
}
catch (const std::filesystem::filesystem_error& err)
{
fmt::println("failed: {}", err.what());
ServerInstance->Exit(EXIT_STATUS_MODULE);
ServerInstance->Exit(EXIT_FAILURE);
}
fmt::println("");

View File

@ -33,7 +33,6 @@
#include <fmt/color.h>
#include "inspircd.h"
#include "exitcodes.h"
#include "dynamic.h"
#include "utility/map.h"
@ -540,7 +539,7 @@ void ModuleManager::LoadAll()
fmt::println("");
fmt::println("[{}] {}", fmt::styled("*", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), LastError());
fmt::println("");
ServerInstance->Exit(EXIT_STATUS_MODULE);
ServerInstance->Exit(EXIT_FAILURE);
}
}
@ -561,7 +560,7 @@ void ModuleManager::LoadAll()
fmt::println("");
fmt::println("[{}] {}", fmt::styled("*", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), LastModuleError);
fmt::println("");
ServerInstance->Exit(EXIT_STATUS_MODULE);
ServerInstance->Exit(EXIT_FAILURE);
}
}
@ -585,12 +584,12 @@ void ModuleManager::LoadAll()
fmt::println("");
fmt::println("[{}] {}", fmt::styled("*", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), LastModuleError);
fmt::println("");
ServerInstance->Exit(EXIT_STATUS_CONFIG);
ServerInstance->Exit(EXIT_FAILURE);
}
}
if (!PrioritizeHooks())
ServerInstance->Exit(EXIT_STATUS_MODULE);
ServerInstance->Exit(EXIT_FAILURE);
}
std::string& ModuleManager::LastError()

View File

@ -27,14 +27,13 @@
#include "inspircd.h"
#include "exitcodes.h"
void InspIRCd::SignalHandler(int signal)
{
switch (signal)
{
case SIGTERM:
Exit(EXIT_STATUS_SIGTERM);
Exit(EXIT_FAILURE);
#ifndef _WIN32
case SIGHUP:
@ -63,12 +62,7 @@ void InspIRCd::Exit(int status)
void InspIRCd::QuickExit(int status)
{
#ifdef INSPIRCD_BINARY_EXIT
// Some init systems handle non-binary exit statuses weirdly.
exit(status ? EXIT_FAILURE : EXIT_SUCCESS);
#else
exit(status);
#endif
}
void InspIRCd::Rehash(const std::string& uuid)

View File

@ -35,7 +35,6 @@
#include <fmt/color.h>
#include "inspircd.h"
#include "exitcodes.h"
/** Reference table, contains all current handlers
**/
@ -71,7 +70,7 @@ void EventHandler::OnEventHandlerError(int errornum)
void SocketEngine::InitError()
{
fmt::println(stderr, "{} Socket engine initialization failed. {}.", fmt::styled("FATAL ERROR!", fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), strerror(errno));
InspIRCd::QuickExit(EXIT_STATUS_SOCKETENGINE);
InspIRCd::QuickExit(EXIT_FAILURE);
}
void SocketEngine::LookupMaxFds()

View File

@ -112,9 +112,6 @@ private:
DWORD dwErrorCode;
};
// Same value as EXIT_STATUS_FORK (EXIT_STATUS_FORK is unused on Windows)
#define EXIT_STATUS_SERVICE 4
// POSIX iovec
struct iovec final
{

View File

@ -22,7 +22,6 @@
#include "inspircd.h"
#include "exitcodes.h"
#include <iostream>
@ -61,14 +60,14 @@ void SetServiceRunning()
throw CWin32Exception();
}
/* In windows we hook this to InspIRCd::Exit() */
/* In windows we hook this to InspIRCd::Exit(EXIT_FAILURE) */
void SetServiceStopped(DWORD dwStatus)
{
if (!g_bRunningAsService)
return;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
if(dwStatus != EXIT_STATUS_NOERROR)
if(dwStatus != EXIT_SUCCESS)
{
g_ServiceStatus.dwServiceSpecificExitCode = dwStatus;
g_ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
@ -255,12 +254,12 @@ int main(int argc, char* argv[])
if(!_stricmp(argv[i], "--installservice"))
{
InstallService();
return 0;
return EXIT_SUCCESS;
}
if(!_stricmp(argv[i], "--uninstallservice") || !_stricmp(argv[i], "--removeservice"))
{
UninstallService();
return 0;
return EXIT_SUCCESS;
}
}
}
@ -282,8 +281,8 @@ int main(int argc, char* argv[])
}
else
{
return EXIT_STATUS_SERVICE;
return EXIT_FAILURE;
}
}
return 0;
return EXIT_SUCCESS;
}