diff --git a/dCommon/Process.cpp b/dCommon/Process.cpp index 70b29efca..2b77ae013 100644 --- a/dCommon/Process.cpp +++ b/dCommon/Process.cpp @@ -8,6 +8,8 @@ #else #include #include +#include +#include #include extern char** environ; #endif @@ -26,6 +28,8 @@ namespace Process { } #ifdef _WIN32 + void CloseInheritedFds(int) {} + int Run(const std::vector& 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(status); } #else + void CloseInheritedFds(int first) { +#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34)) + if (close_range(static_cast(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(std::min(limit.rlim_cur, 65536)); + for (int fd = first; fd < last; fd++) close(fd); + } + int Run(const std::vector& arguments, const std::string& stdoutFile, const std::string& stderrFile) { if (arguments.empty()) return -1; std::vector 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) { diff --git a/dCommon/Process.h b/dCommon/Process.h index 60aa3d000..5e698bd5e 100644 --- a/dCommon/Process.h +++ b/dCommon/Process.h @@ -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); } diff --git a/dMasterServer/Start.cpp b/dMasterServer/Start.cpp index 024abb005..a8bb23f00 100644 --- a/dMasterServer/Start.cpp +++ b/dMasterServer/Start.cpp @@ -3,6 +3,7 @@ #include "dConfig.h" #include "Game.h" #include "BinaryPathFinder.h" +#include "Process.h" #ifdef _WIN32 #include @@ -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(), diff --git a/tests/dCommonTests/CMakeLists.txt b/tests/dCommonTests/CMakeLists.txt index 94a5ba44c..11eac6117 100644 --- a/tests/dCommonTests/CMakeLists.txt +++ b/tests/dCommonTests/CMakeLists.txt @@ -34,6 +34,7 @@ set(DCOMMONTEST_SOURCES "PropertyRentRulesTests.cpp" "PropertyReputationRulesTests.cpp" "BindAddressTests.cpp" + "ProcessFdTests.cpp" "TrafficStatsTests.cpp" "ProfilerTests.cpp" "Sd0Tests.cpp" diff --git a/tests/dCommonTests/ProcessFdTests.cpp b/tests/dCommonTests/ProcessFdTests.cpp new file mode 100644 index 000000000..f3853140f --- /dev/null +++ b/tests/dCommonTests/ProcessFdTests.cpp @@ -0,0 +1,44 @@ +#include + +#include "Process.h" + +#ifdef __linux__ +#include +#include +#include +#include + +namespace { + // Starts `sh -c