mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
fix(master): servers master starts no longer inherit its sockets
The child closes every descriptor above 2 before exec, so master's UDP ports are released when master stops. Process::Run closes them too. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <spawn.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
extern char** environ;
|
||||
#endif
|
||||
@@ -26,6 +28,8 @@ namespace Process {
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
void CloseInheritedFds(int) {}
|
||||
|
||||
int Run(const std::vector<std::string>& arguments, const std::string& stdoutFile, const std::string& stderrFile) {
|
||||
if (arguments.empty()) return -1;
|
||||
// _spawnvp takes an argument vector; cmd.exe is not involved. Output redirection is not supported here.
|
||||
@@ -42,6 +46,16 @@ namespace Process {
|
||||
return status < 0 ? -1 : static_cast<int>(status);
|
||||
}
|
||||
#else
|
||||
void CloseInheritedFds(int first) {
|
||||
#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34))
|
||||
if (close_range(static_cast<unsigned int>(first), ~0U, 0) == 0) return;
|
||||
#endif
|
||||
rlimit limit{};
|
||||
int last = 4096;
|
||||
if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY) last = static_cast<int>(std::min<rlim_t>(limit.rlim_cur, 65536));
|
||||
for (int fd = first; fd < last; fd++) close(fd);
|
||||
}
|
||||
|
||||
int Run(const std::vector<std::string>& arguments, const std::string& stdoutFile, const std::string& stderrFile) {
|
||||
if (arguments.empty()) return -1;
|
||||
std::vector<char*> argv;
|
||||
@@ -56,6 +70,10 @@ namespace Process {
|
||||
};
|
||||
pid_t pid = 0;
|
||||
int status = -1;
|
||||
#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34))
|
||||
// The program does not get this process's sockets
|
||||
posix_spawn_file_actions_addclosefrom_np(&actions, 3);
|
||||
#endif
|
||||
if (posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_RDONLY, 0) == 0 &&
|
||||
redirect(1, stdoutFile) == 0 && redirect(2, stderrFile) == 0 &&
|
||||
posix_spawnp(&pid, argv[0], &actions, nullptr, argv.data(), environ) == 0) {
|
||||
|
||||
@@ -20,4 +20,12 @@ namespace Process {
|
||||
// Parentheses, spaces and backslashes are allowed: they are common in real folder names.
|
||||
// Used to refuse suspicious paths even though Run never uses a shell.
|
||||
bool HasShellMetacharacters(std::string_view text);
|
||||
|
||||
/**
|
||||
* For a forked child just before exec: closes every file descriptor from `first` up, so the program it starts
|
||||
* does not keep the parent's sockets (a listening port stays held after the parent stops otherwise).
|
||||
* Only calls close_range or close, so it is safe between fork and exec. Does nothing on Windows, where
|
||||
* CreateProcess is called without handle inheritance.
|
||||
*/
|
||||
void CloseInheritedFds(int first = 3);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "dConfig.h"
|
||||
#include "Game.h"
|
||||
#include "BinaryPathFinder.h"
|
||||
#include "Process.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -65,6 +66,8 @@ uint32_t StartChatServer() {
|
||||
return 0;
|
||||
} else if (chat_pid == 0) {
|
||||
// We are the child process
|
||||
// Not the master's sockets: they would stay open in the child and hold master's ports after master stops
|
||||
Process::CloseInheritedFds();
|
||||
execl(chat_path.string().c_str(), chat_path.string().c_str(), nullptr);
|
||||
// exec failed (e.g. the binary is being rebuilt): the child must not carry on as a second master
|
||||
perror("exec");
|
||||
@@ -104,6 +107,8 @@ uint32_t StartAuthServer() {
|
||||
return 0;
|
||||
} else if (auth_pid == 0) {
|
||||
// We are the child process
|
||||
// Not the master's sockets: they would stay open in the child and hold master's ports after master stops
|
||||
Process::CloseInheritedFds();
|
||||
execl(auth_path.string().c_str(), auth_path.string().c_str(), nullptr);
|
||||
// exec failed (e.g. the binary is being rebuilt): the child must not carry on as a second master
|
||||
perror("exec");
|
||||
@@ -143,6 +148,8 @@ uint32_t StartDashboardServer() {
|
||||
return 0;
|
||||
} else if (web_pid == 0) {
|
||||
// We are the child process
|
||||
// Not the master's sockets: they would stay open in the child and hold master's ports after master stops
|
||||
Process::CloseInheritedFds();
|
||||
execl(web_path.string().c_str(), web_path.string().c_str(), nullptr);
|
||||
// exec failed (e.g. the binary is being rebuilt): the child must not carry on as a second master
|
||||
perror("exec");
|
||||
@@ -182,6 +189,8 @@ uint32_t StartUgcServer() {
|
||||
return 0;
|
||||
} else if (ugc_pid == 0) {
|
||||
// We are the child process
|
||||
// Not the master's sockets: they would stay open in the child and hold master's ports after master stops
|
||||
Process::CloseInheritedFds();
|
||||
execl(ugc_path.string().c_str(), ugc_path.string().c_str(), nullptr);
|
||||
// exec failed (e.g. the binary is being rebuilt): the child must not carry on as a second master
|
||||
perror("exec");
|
||||
@@ -221,6 +230,8 @@ uint32_t StartWorldServer(LWOMAPID mapID, uint16_t port, LWOINSTANCEID lastInsta
|
||||
return 0;
|
||||
} else if (world_pid == 0) {
|
||||
// We are the child process
|
||||
// Not the master's sockets: they would stay open in the child and hold master's ports after master stops
|
||||
Process::CloseInheritedFds();
|
||||
execl(world_path.string().c_str(), world_path.string().c_str(),
|
||||
"-zone", std::to_string(mapID).c_str(),
|
||||
"-port", std::to_string(port).c_str(),
|
||||
|
||||
@@ -34,6 +34,7 @@ set(DCOMMONTEST_SOURCES
|
||||
"PropertyRentRulesTests.cpp"
|
||||
"PropertyReputationRulesTests.cpp"
|
||||
"BindAddressTests.cpp"
|
||||
"ProcessFdTests.cpp"
|
||||
"TrafficStatsTests.cpp"
|
||||
"ProfilerTests.cpp"
|
||||
"Sd0Tests.cpp"
|
||||
|
||||
44
tests/dCommonTests/ProcessFdTests.cpp
Normal file
44
tests/dCommonTests/ProcessFdTests.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Process.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
// Starts `sh -c <script>` the way master starts a server; returns its exit code
|
||||
int SpawnLikeMaster(const std::string& script, const bool closeFds) {
|
||||
const auto pid = fork();
|
||||
if (pid == 0) {
|
||||
if (closeFds) Process::CloseInheritedFds();
|
||||
execl("/bin/sh", "sh", "-c", script.c_str(), nullptr);
|
||||
_exit(127);
|
||||
}
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ProcessFdTests, ChildDoesNotKeepTheParentsSockets) {
|
||||
const int sock = socket(AF_INET, SOCK_DGRAM, 0); // no close-on-exec, like a socket a library opens
|
||||
ASSERT_GE(sock, 3);
|
||||
const auto check = "[ -e /proc/self/fd/" + std::to_string(sock) + " ]";
|
||||
// Without the close the program still holds it (the check passes: exit 0), with it the fd is gone (exit 1)
|
||||
EXPECT_EQ(SpawnLikeMaster(check, false), 0);
|
||||
EXPECT_EQ(SpawnLikeMaster(check, true), 1);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
TEST(ProcessFdTests, RunDoesNotKeepTheParentsSockets) {
|
||||
const int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
ASSERT_GE(sock, 3);
|
||||
// sh -c 'ls /proc/self/fd' would list the fds; the program must only see 0, 1, 2 and the one ls opens
|
||||
const auto status = Process::Run({ "/bin/sh", "-c", "[ ! -e /proc/self/fd/" + std::to_string(sock) + " ]" });
|
||||
EXPECT_EQ(status, 0);
|
||||
close(sock);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user