mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-13 01:08:35 +00:00
feat(Core): Linux: Support systemd socket activation for the worldserver game socket (#21998)
This commit is contained in:
59
src/common/Utilities/Systemd.cpp
Normal file
59
src/common/Utilities/Systemd.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information.
|
||||
*
|
||||
* Portions of this file are derived from systemd, licensed under:
|
||||
* - GPL-2.0-or-later (if the original systemd file was GPL-2.0+)
|
||||
* OR
|
||||
* - LGPL-2.1-or-later, relicensed under GPL-2.0-or-later as permitted by LGPL-2.1+
|
||||
*
|
||||
* Original systemd copyright:
|
||||
* Copyright (c) the systemd contributors.
|
||||
*
|
||||
* This program 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; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#if defined(__linux__)
|
||||
#include "Log.h"
|
||||
#include "StringConvert.h"
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
|
||||
int get_listen_fd()
|
||||
{
|
||||
char* const listen_pid = std::getenv("LISTEN_PID");
|
||||
char* const listen_fds = std::getenv("LISTEN_FDS");
|
||||
if (!listen_pid || !listen_fds)
|
||||
return 0;
|
||||
|
||||
pid_t pid = Acore::StringTo<int>(listen_pid).value_or(0);
|
||||
if (pid != getpid())
|
||||
return 0;
|
||||
|
||||
int fds = Acore::StringTo<int>(listen_fds).value_or(0);
|
||||
if (fds <= 0)
|
||||
return 0;
|
||||
|
||||
if (fds > 1)
|
||||
LOG_WARN("network", "Multiple file descriptors received from systemd socket activation, only the first will be used");
|
||||
|
||||
return 3;
|
||||
}
|
||||
#else
|
||||
// On non-Linux systems, just return 0 (no socket activation)
|
||||
int get_listen_fd()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
31
src/common/Utilities/Systemd.h
Normal file
31
src/common/Utilities/Systemd.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information.
|
||||
*
|
||||
* Portions of this file are derived from systemd, licensed under:
|
||||
* - GPL-2.0-or-later (if the original systemd file was GPL-2.0+)
|
||||
* OR
|
||||
* - LGPL-2.1-or-later, relicensed under GPL-2.0-or-later as permitted by LGPL-2.1+
|
||||
*
|
||||
* Original systemd copyright:
|
||||
* Copyright (c) the systemd contributors.
|
||||
*
|
||||
* This program 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; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _SYSTEMD_H_
|
||||
#define _SYSTEMD_H_
|
||||
|
||||
int get_listen_fd();
|
||||
|
||||
#endif
|
||||
@@ -48,6 +48,7 @@
|
||||
#include "SecretMgr.h"
|
||||
#include "SharedDefines.h"
|
||||
#include "SteadyTimer.h"
|
||||
#include "Systemd.h"
|
||||
#include "World.h"
|
||||
#include "WorldSessionMgr.h"
|
||||
#include "WorldSocket.h"
|
||||
@@ -406,7 +407,8 @@ int main(int argc, char** argv)
|
||||
sScriptMgr->OnShutdown();
|
||||
|
||||
// set server offline
|
||||
LoginDatabase.DirectExecute("UPDATE realmlist SET flag = flag | {} WHERE id = '{}'", REALM_FLAG_OFFLINE, realm.Id.Realm);
|
||||
if (!sConfigMgr->GetOption<bool>("Network.UseSocketActivation", false))
|
||||
LoginDatabase.DirectExecute("UPDATE realmlist SET flag = flag | {} WHERE id = '{}'", REALM_FLAG_OFFLINE, realm.Id.Realm);
|
||||
|
||||
LOG_INFO("server.worldserver", "Halting process...");
|
||||
|
||||
|
||||
@@ -397,6 +397,20 @@ Network.TcpNodelay = 1
|
||||
|
||||
Network.EnableProxyProtocol = 0
|
||||
|
||||
#
|
||||
# Network.UseSocketActivation
|
||||
# Description: Enable systemd socket activation support for the worldserver.
|
||||
# When enabled and the process is started by systemd socket activation,
|
||||
# the server will use the socket passed by systemd instead of
|
||||
# creating and binding its own listening socket. Disabled by default.
|
||||
#
|
||||
# When enabled the realm is not automatically set as offline on shutdown.
|
||||
#
|
||||
# Example: 1 - (Enabled)
|
||||
# Default: 0 - (Disabled)
|
||||
|
||||
Network.UseSocketActivation = 0
|
||||
|
||||
#
|
||||
###################################################################################################
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "IpAddress.h"
|
||||
#include "Log.h"
|
||||
#include "Systemd.h"
|
||||
#include <atomic>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <functional>
|
||||
@@ -33,10 +34,20 @@ class AsyncAcceptor
|
||||
public:
|
||||
typedef void(*AcceptCallback)(tcp::socket&& newSocket, uint32 threadIndex);
|
||||
|
||||
AsyncAcceptor(Acore::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port) :
|
||||
AsyncAcceptor(Acore::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, bool supportSocketActivation = false) :
|
||||
_acceptor(ioContext), _endpoint(Acore::Net::make_address(bindIp), port),
|
||||
_socket(ioContext), _closed(false), _socketFactory([this](){ return DefaultSocketFactory(); })
|
||||
_socket(ioContext), _closed(false), _socketFactory([this](){ return DefaultSocketFactory(); }),
|
||||
_supportSocketActivation(supportSocketActivation)
|
||||
{
|
||||
int const listen_fd = get_listen_fd();
|
||||
if (_supportSocketActivation && listen_fd > 0)
|
||||
{
|
||||
LOG_DEBUG("network", "Using socket from systemd socket activation");
|
||||
boost::system::error_code errorCode;
|
||||
_acceptor.assign(boost::asio::ip::tcp::v4(), listen_fd, errorCode);
|
||||
if (errorCode)
|
||||
LOG_WARN("network", "Failed to assign socket {}", errorCode.message());
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
@@ -72,27 +83,31 @@ public:
|
||||
bool Bind()
|
||||
{
|
||||
boost::system::error_code errorCode;
|
||||
_acceptor.open(_endpoint.protocol(), errorCode);
|
||||
if (errorCode)
|
||||
// with socket activation the acceptor is already open and bound
|
||||
if (!_acceptor.is_open())
|
||||
{
|
||||
LOG_INFO("network", "Failed to open acceptor {}", errorCode.message());
|
||||
return false;
|
||||
}
|
||||
_acceptor.open(_endpoint.protocol(), errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
LOG_INFO("network", "Failed to open acceptor {}", errorCode.message());
|
||||
return false;
|
||||
}
|
||||
|
||||
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
|
||||
_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true), errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
LOG_INFO("network", "Failed to set reuse_address option on acceptor {}", errorCode.message());
|
||||
return false;
|
||||
}
|
||||
_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true), errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
LOG_INFO("network", "Failed to set reuse_address option on acceptor {}", errorCode.message());
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
_acceptor.bind(_endpoint, errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
LOG_INFO("network", "Could not bind to {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message());
|
||||
return false;
|
||||
_acceptor.bind(_endpoint, errorCode);
|
||||
if (errorCode)
|
||||
{
|
||||
LOG_INFO("network", "Could not bind to {}:{} {}", _endpoint.address().to_string(), _endpoint.port(), errorCode.message());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_acceptor.listen(ACORE_MAX_LISTEN_CONNECTIONS, errorCode);
|
||||
@@ -124,6 +139,7 @@ private:
|
||||
tcp::socket _socket;
|
||||
std::atomic<bool> _closed;
|
||||
std::function<std::pair<tcp::socket*, uint32>()> _socketFactory;
|
||||
bool _supportSocketActivation;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#define SocketMgr_h__
|
||||
|
||||
#include "AsyncAcceptor.h"
|
||||
#include "Config.h"
|
||||
#include "Errors.h"
|
||||
#include "NetworkThread.h"
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
@@ -42,7 +43,8 @@ public:
|
||||
std::unique_ptr<AsyncAcceptor> acceptor;
|
||||
try
|
||||
{
|
||||
acceptor = std::make_unique<AsyncAcceptor>(ioContext, bindIp, port);
|
||||
bool supportSocketActivation = sConfigMgr->GetOption<bool>("Network.UseSocketActivation", false);
|
||||
acceptor = std::make_unique<AsyncAcceptor>(ioContext, bindIp, port, supportSocketActivation);
|
||||
}
|
||||
catch (boost::system::system_error const& err)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user