diff options
Diffstat (limited to 'src/zenutil')
| -rw-r--r-- | src/zenutil/chunkrequests.cpp | 2 | ||||
| -rw-r--r-- | src/zenutil/include/zenutil/service.h | 1 | ||||
| -rw-r--r-- | src/zenutil/service.cpp | 63 | ||||
| -rw-r--r-- | src/zenutil/zenserverprocess.cpp | 14 |
4 files changed, 64 insertions, 16 deletions
diff --git a/src/zenutil/chunkrequests.cpp b/src/zenutil/chunkrequests.cpp index 745363668..e28df02a8 100644 --- a/src/zenutil/chunkrequests.cpp +++ b/src/zenutil/chunkrequests.cpp @@ -1,5 +1,7 @@ // Copyright Epic Games, Inc. All Rights Reserved. +#include <algorithm> + #include <zenutil/chunkrequests.h> #include <zencore/blake3.h> diff --git a/src/zenutil/include/zenutil/service.h b/src/zenutil/include/zenutil/service.h index 492e5c80a..2798bcb1f 100644 --- a/src/zenutil/include/zenutil/service.h +++ b/src/zenutil/include/zenutil/service.h @@ -20,6 +20,7 @@ struct ServiceSpec { std::filesystem::path ExecutablePath; std::string CommandLineOptions; + std::string UserName; #if ZEN_PLATFORM_WINDOWS std::string DisplayName; std::string Description; diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp index 45874d1b5..ea7c2aae6 100644 --- a/src/zenutil/service.cpp +++ b/src/zenutil/service.cpp @@ -6,6 +6,7 @@ #include <zencore/process.h> #include <zencore/scopeguard.h> #include <zencore/zencore.h> +#include <string_view> #if ZEN_PLATFORM_WINDOWS # include <zencore/windows.h> @@ -23,6 +24,7 @@ # include <unistd.h> # include <sys/stat.h> +# include <regex> #endif namespace zen { @@ -258,7 +260,6 @@ namespace { #if ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX - // TODO: Is this good enough to capture all output/errors/return codes? std::pair<int, std::string> ExecuteProgram(std::string_view Cmd) { std::string Data; @@ -289,10 +290,11 @@ namespace { int Status = pclose(Stream); if (Status < 0) { + ZEN_DEBUG("Command {} returned {}, errno {}", Command, Status, errno); return {Status, Data}; } uint64_t WaitMS = 100; - if (!WIFEXITED(Status)) + if (WIFEXITED(Status)) { Res = WEXITSTATUS(Status); } @@ -319,10 +321,9 @@ namespace { std::string BuildUnitFile(std::string_view ServiceName, const std::filesystem::path& ExecutablePath, std::string_view CommandLineOptions, - std::string_view AliasName) + std::string_view AliasName, + std::string_view UserName) { - // TODO: Revise to make sure the unit file is correct - // TODO: Do we need a separate config file or is that optional? return fmt::format( "[Unit]\n" "Description={}\n" @@ -333,17 +334,17 @@ namespace { "StartLimitIntervalSec=0\n" "\n" "[Service]\n" - "Type=simple\n" + "Type=notify\n" "Restart=always\n" "RestartSec=1\n" - "User=serviceuser\n" + "User={}\n" "ExecStart={} {}\n" - "Restart=always\n" "RuntimeDirectory={}\n" "[Install]\n" "Alias={}\n" "WantedBy=multi-user.target", ServiceName, + UserName, ExecutablePath, CommandLineOptions, ExecutablePath.parent_path(), @@ -881,12 +882,23 @@ StopService(std::string_view ServiceName) std::error_code InstallService(std::string_view ServiceName, const ServiceSpec& Spec) { - // TODO: Do we need to create a separate user for the service or is running as root OK? - const std::string UnitName = GetUnitName(ServiceName); const std::filesystem::path ServiceUnitPath = GetServiceUnitPath(UnitName); + std::string UserName = Spec.UserName; + + if (UserName == "") + { + std::pair<int, std::string> UserResult = ExecuteProgram("echo $SUDO_USER"); + if (UserResult.first != 0 || UserResult.second.empty()) + { + ZEN_ERROR("Unable to determine current user"); + return MakeErrorCode(UserResult.first); + } + + UserName = UserResult.second; + } - std::string UnitFile = BuildUnitFile(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, UnitName); + std::string UnitFile = BuildUnitFile(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, UnitName, UserName); ZEN_DEBUG("Writing systemd unit file to {}", ServiceUnitPath.string()); try { @@ -963,17 +975,38 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) if (std::filesystem::is_regular_file(ServiceUnitPath)) { OutInfo.Status = ServiceStatus::Stopped; - // TODO: Read and parse unit file ? - std::pair<int, std::string> Res = ExecuteProgram(fmt::format("systemctl status {}", UnitName)); + std::pair<int, std::string> Res = ExecuteProgram(fmt::format("systemctl is-active --quiet {}", UnitName)); if (Res.first == 0) { - // TODO: What does status really return and what info can we use here to get the ServiceInfo complete? OutInfo.Status = ServiceStatus::Running; + + std::pair<int, std::string> ShowResult = ExecuteProgram(fmt::format("systemctl show -p ExecStart {}", UnitName)); + if (ShowResult.first == 0) + { + std::regex Regex(R"~(ExecStart=\{ path=(.*?) ; argv\[\]=(.*?) ;)~"); + std::smatch Match; + + if (std::regex_search(ShowResult.second, Match, Regex)) + { + std::string Executable = Match[1].str(); + std::string CommandLine = Match[2].str(); + OutInfo.Spec.ExecutablePath = Executable; + OutInfo.Spec.CommandLineOptions = CommandLine.substr(Executable.size(), CommandLine.size()); + } + else + { + ZEN_WARN("Failed to parse output of systemctl show: {}", ShowResult.second); + } + } + else + { + ZEN_WARN("Failed to read start info from systemctl: error code {}", ShowResult.first); + } } else { - ZEN_DEBUG("systemctl status failed with '{}"({}), Res.second, Res.first); + ZEN_DEBUG("systemctl status failed with '{}'({})", Res.second, Res.first); } } diff --git a/src/zenutil/zenserverprocess.cpp b/src/zenutil/zenserverprocess.cpp index 11fcce02f..0409cb976 100644 --- a/src/zenutil/zenserverprocess.cpp +++ b/src/zenutil/zenserverprocess.cpp @@ -168,10 +168,22 @@ ZenServerState::Initialize() } #else ZEN_INFO("{}", S_IRUSR | S_IWUSR | S_IXUSR); + ZEN_INFO("{}", geteuid()); int Fd = shm_open("/UnrealEngineZen", O_RDWR | O_CREAT | O_CLOEXEC, geteuid() == 0 ? 0766 : 0666); if (Fd < 0) { - ThrowLastError("Could not open a shared memory object"); + // Work around a potential issue if the service user is changed in certain configurations. + // If the sysctl 'fs.protected_regular' is set to 1 or 2 (default on many distros), + // we will be unable to open an existing shared memory object created by another user using O_CREAT, + // even if we have the correct permissions, or are running as root. If we destroy the existing + // shared memory object and retry, we'll be able to get past shm_open() so long as we have + // the appropriate permissions to create the shared memory object. + shm_unlink("/UnrealEngineZen"); + Fd = shm_open("/UnrealEngineZen", O_RDWR | O_CREAT | O_CLOEXEC, geteuid() == 0 ? 0766 : 0666); + if (Fd < 0) + { + ThrowLastError("Could not open a shared memory object"); + } } fchmod(Fd, 0666); void* hMap = (void*)intptr_t(Fd); |