aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2025-01-10 15:53:34 +0100
committerDan Engelbrecht <[email protected]>2025-01-10 15:53:34 +0100
commit6aeec009d348a6bef2923f6eebb9997dfd363cdf (patch)
treeb42b9766f4355723b930be1104d40a9b9fd79423 /src
parentdisplayname and description are windows-only properties (diff)
downloadzen-6aeec009d348a6bef2923f6eebb9997dfd363cdf.tar.xz
zen-6aeec009d348a6bef2923f6eebb9997dfd363cdf.zip
clang format
Diffstat (limited to 'src')
-rw-r--r--src/zen/cmds/service_cmd.cpp54
-rw-r--r--src/zen/cmds/service_cmd.h6
-rw-r--r--src/zenutil/include/zenutil/service.h6
-rw-r--r--src/zenutil/service.cpp119
4 files changed, 91 insertions, 94 deletions
diff --git a/src/zen/cmds/service_cmd.cpp b/src/zen/cmds/service_cmd.cpp
index db60d9b54..5011ac12f 100644
--- a/src/zen/cmds/service_cmd.cpp
+++ b/src/zen/cmds/service_cmd.cpp
@@ -98,7 +98,7 @@ namespace {
#else // ZEN_PLATFORM_WINDOWS
- bool IsElevated() { return true;/*geteuid() == 0;*/ }
+ bool IsElevated() { return true; /*geteuid() == 0;*/ }
#endif // ZEN_PLATFORM_WINDOWS
@@ -240,25 +240,32 @@ ServiceCommand::ServiceCommand()
ServiceCommand::~ServiceCommand() = default;
-std::string FmtServiceInfo(const ServiceInfo& Info, std::string_view Prefix)
+std::string
+FmtServiceInfo(const ServiceInfo& Info, std::string_view Prefix)
{
std::string Result = fmt::format(
"{}Status: {}\n"
"{}Executable: {}\n"
"{}CommandLineOptions: {}",
- Prefix, ToString(Info.Status),
- Prefix, Info.Spec.ExecutablePath,
- Prefix, Info.Spec.CommandLineOptions);
+ Prefix,
+ ToString(Info.Status),
+ Prefix,
+ Info.Spec.ExecutablePath,
+ Prefix,
+ Info.Spec.CommandLineOptions);
#if ZEN_PLATFORM_WINDOWS
- Result += fmt::format("\n"
+ Result += fmt::format(
+ "\n"
"{}Display Name: {}\n"
"{}Description: {}",
- Prefix, Info.Spec.DisplayName,
- Prefix, Info.Spec.Description);
-#endif // ZEN_PLATFORM_WINDOWS
+ Prefix,
+ Info.Spec.DisplayName,
+ Prefix,
+ Info.Spec.Description);
+#endif // ZEN_PLATFORM_WINDOWS
- return Result;
+ return Result;
}
int
@@ -302,10 +309,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
}
else
{
- ZEN_CONSOLE(
- "Service '{}':\n{}",
- m_ServiceName,
- FmtServiceInfo(Info, " "));
+ ZEN_CONSOLE("Service '{}':\n{}", m_ServiceName, FmtServiceInfo(Info, " "));
}
}
@@ -315,10 +319,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
if (!Ec && Info.Status != ServiceStatus::NotInstalled)
{
- ZEN_CONSOLE(
- "Service '{}' already installed:\n{}",
- m_ServiceName,
- FmtServiceInfo(Info, " "));
+ ZEN_CONSOLE("Service '{}' already installed:\n{}", m_ServiceName, FmtServiceInfo(Info, " "));
return 1;
}
@@ -334,14 +335,15 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
m_ServerExecutable = ExePath;
}
m_ServerExecutable = std::filesystem::absolute(m_ServerExecutable);
- Ec = InstallService(m_ServiceName,
- ServiceSpec{.ExecutablePath = m_ServerExecutable,
- .CommandLineOptions = GlobalOptions.PassthroughCommandLine
- #if ZEN_PLATFORM_WINDOWS
- ,.DisplayName = m_ServiceDisplayName
- ,.Description = m_ServiceDescription
- #endif // ZEN_PLATFORM_WINDOWS
- });
+ Ec = InstallService(
+ m_ServiceName,
+ ServiceSpec {
+ .ExecutablePath = m_ServerExecutable, .CommandLineOptions = GlobalOptions.PassthroughCommandLine
+#if ZEN_PLATFORM_WINDOWS
+ ,
+ .DisplayName = m_ServiceDisplayName, .Description = m_ServiceDescription
+#endif // ZEN_PLATFORM_WINDOWS
+ });
if (Ec)
{
ZEN_CONSOLE("Failed to install service '{}' using '{}' . Reason: '{}'", m_ServiceName, m_ServerExecutable, Ec.message());
diff --git a/src/zen/cmds/service_cmd.h b/src/zen/cmds/service_cmd.h
index 00a442048..4d370b29c 100644
--- a/src/zen/cmds/service_cmd.h
+++ b/src/zen/cmds/service_cmd.h
@@ -36,9 +36,9 @@ private:
"Install zenserver as a service. Arguments following \" -- \" will be added as parameters to the installed service."};
std::filesystem::path m_ServerExecutable;
#if ZEN_PLATFORM_WINDOWS
- std::string m_ServiceDisplayName = "Unreal Zen Storage Service";
- std::string m_ServiceDescription;
-#endif // ZEN_PLATFORM_WINDOWS
+ std::string m_ServiceDisplayName = "Unreal Zen Storage Service";
+ std::string m_ServiceDescription;
+#endif // ZEN_PLATFORM_WINDOWS
cxxopts::Options m_UninstallOptions{"uninstall", "Uninstall zenserver as a service"};
diff --git a/src/zenutil/include/zenutil/service.h b/src/zenutil/include/zenutil/service.h
index 1e96031f7..492e5c80a 100644
--- a/src/zenutil/include/zenutil/service.h
+++ b/src/zenutil/include/zenutil/service.h
@@ -21,9 +21,9 @@ struct ServiceSpec
std::filesystem::path ExecutablePath;
std::string CommandLineOptions;
#if ZEN_PLATFORM_WINDOWS
- std::string DisplayName;
- std::string Description;
-#endif // ZEN_PLATFORM_WINDOWS
+ std::string DisplayName;
+ std::string Description;
+#endif // ZEN_PLATFORM_WINDOWS
};
enum class ServiceStatus
diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp
index f85a8e7e3..cfb19e9a9 100644
--- a/src/zenutil/service.cpp
+++ b/src/zenutil/service.cpp
@@ -173,10 +173,7 @@ namespace {
}
}
- std::string GetDaemonName(std::string_view ServiceName)
- {
- return fmt::format("com.epicgames.unreal.{}", ServiceName);
- }
+ std::string GetDaemonName(std::string_view ServiceName) { return fmt::format("com.epicgames.unreal.{}", ServiceName); }
std::filesystem::path GetPListPath(const std::string& DaemonName)
{
@@ -188,7 +185,7 @@ namespace {
const std::filesystem::path& ExecutablePath,
std::string_view CommandLineOptions,
std::string_view DaemonName,
- bool Debug)
+ bool Debug)
{
std::vector<std::string_view> Arguments = SplitArguments(CommandLineOptions);
ExtendableStringBuilder<256> ProgramArguments;
@@ -253,36 +250,36 @@ namespace {
#endif // ZEN_PLATFORM_MAC
-
#if ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX
-std::pair<int, std::string> ExecuteProgram(std::string_view Cmd)
-{
- std::string data;
- const int max_buffer = 256;
- char buffer[max_buffer];
- std::string Command(Cmd);
- Command.append(" 2>&1");
-
- FILE * stream = popen(Command.c_str(), "r");
- if (stream)
+ std::pair<int, std::string> ExecuteProgram(std::string_view Cmd)
{
- while (!feof(stream))
+ std::string data;
+ const int max_buffer = 256;
+ char buffer[max_buffer];
+ std::string Command(Cmd);
+ Command.append(" 2>&1");
+
+ FILE* stream = popen(Command.c_str(), "r");
+ if (stream)
{
- if (fgets(buffer, max_buffer, stream) != NULL)
+ while (!feof(stream))
{
- data.append(buffer);
+ if (fgets(buffer, max_buffer, stream) != NULL)
+ {
+ data.append(buffer);
+ }
}
- }
- int Res = -1;
- int st = pclose(stream);
- if (WIFEXITED(st)) Res = WEXITSTATUS(st);
- return {Res, data};
- }
- return {errno, ""};
+ int Res = -1;
+ int st = pclose(stream);
+ if (WIFEXITED(st))
+ Res = WEXITSTATUS(st);
+ return {Res, data};
+ }
+ return {errno, ""};
-#if 0
+# if 0
int in[2], out[2], n, pid;
char buf[255];
@@ -350,11 +347,10 @@ std::pair<int, std::string> ExecuteProgram(std::string_view Cmd)
std::string Output(buf);
return {0, Output};
-#endif // 0
-}
-
-#endif // ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX
+# endif // 0
+ }
+#endif // ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX
} // namespace
@@ -695,8 +691,7 @@ std::error_code
InstallService(std::string_view ServiceName, const ServiceSpec& Spec)
{
const std::string DaemonName = GetDaemonName(ServiceName);
- std::string PList =
- BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true);
+ std::string PList = BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true);
const std::filesystem::path PListPath = GetPListPath(DaemonName);
ZEN_INFO("Writing launchd plist to {}", PListPath.string());
@@ -720,8 +715,8 @@ InstallService(std::string_view ServiceName, const ServiceSpec& Spec)
std::error_code
UninstallService(std::string_view ServiceName)
{
- const std::string DaemonName = GetDaemonName(ServiceName);
- const std::filesystem::path PListPath = GetPListPath(DaemonName);
+ const std::string DaemonName = GetDaemonName(ServiceName);
+ const std::filesystem::path PListPath = GetPListPath(DaemonName);
ZEN_INFO("Attempting to remove launchd plist from {}", PListPath.string());
std::error_code Ec;
std::filesystem::remove(PListPath, Ec);
@@ -733,7 +728,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
{
ZEN_UNUSED(ServiceName, OutInfo);
- OutInfo.Status = ServiceStatus::NotInstalled;
+ OutInfo.Status = ServiceStatus::NotInstalled;
const std::string DaemonName = GetDaemonName(ServiceName);
const std::filesystem::path PListPath = GetPListPath(DaemonName);
@@ -743,8 +738,8 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
{
// Parse plist :(
- IoBuffer Buffer = ReadFile(PListPath).Flatten();
- MemoryView Data = Buffer.GetView();
+ IoBuffer Buffer = ReadFile(PListPath).Flatten();
+ MemoryView Data = Buffer.GetView();
std::string PList((const char*)Data.GetData(), Data.GetSize());
enum class ParseMode
@@ -757,10 +752,10 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
ParseMode Mode = ParseMode::None;
- ForEachStrTok(PList, '\n', [&](std::string_view Line){
- switch (Mode)
- {
- case ParseMode::None:
+ ForEachStrTok(PList, '\n', [&](std::string_view Line) {
+ switch (Mode)
+ {
+ case ParseMode::None:
{
if (Line.find("<key>ProgramArguments</key>") != std::string_view::npos)
{
@@ -769,7 +764,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
}
}
break;
- case ParseMode::ExpectingProgramArgumentsArray:
+ case ParseMode::ExpectingProgramArgumentsArray:
{
if (Line.find("<array>") != std::string_view::npos)
{
@@ -779,7 +774,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
Mode = ParseMode::None;
}
break;
- case ParseMode::ExpectingProgramExecutablePath:
+ case ParseMode::ExpectingProgramExecutablePath:
{
if (std::string_view::size_type ArgStart = Line.find("<string>"); ArgStart != std::string_view::npos)
{
@@ -787,15 +782,15 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
if (std::string_view::size_type ArgEnd = Line.find("</string>", ArgStart); ArgEnd != std::string_view::npos)
{
std::string_view ProgramString = Line.substr(ArgStart, ArgEnd - ArgStart);
- OutInfo.Spec.ExecutablePath = ProgramString;
- Mode = ParseMode::ExpectingCommandLineOption;
+ OutInfo.Spec.ExecutablePath = ProgramString;
+ Mode = ParseMode::ExpectingCommandLineOption;
return true;
}
}
Mode = ParseMode::None;
}
break;
- case ParseMode::ExpectingCommandLineOption:
+ case ParseMode::ExpectingCommandLineOption:
{
if (std::string_view::size_type ArgStart = Line.find("</array>"); ArgStart != std::string_view::npos)
{
@@ -819,20 +814,20 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo)
Mode = ParseMode::None;
}
break;
- }
- return true;
+ }
+ return true;
});
}
{
- std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl list ") + DaemonName);
+ std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl list ") + DaemonName);
if (Res.first == 0 && !Res.second.empty())
{
- ForEachStrTok(Res.second, '\n', [&](std::string_view Line){
+ ForEachStrTok(Res.second, '\n', [&](std::string_view Line) {
if (Line.find("\"PID\"") != std::string_view::npos)
{
- std::string_view::size_type PidStart = Line.find('=');
- std::string_view::size_type PidEnd = Line.find(';');
- std::string_view PidString = Line.substr(PidStart+2, PidEnd - (PidStart + 2));
+ std::string_view::size_type PidStart = Line.find('=');
+ std::string_view::size_type PidEnd = Line.find(';');
+ std::string_view PidString = Line.substr(PidStart + 2, PidEnd - (PidStart + 2));
if (ParseInt<int>(PidString).has_value())
{
OutInfo.Status = ServiceStatus::Running;
@@ -854,15 +849,15 @@ StartService(std::string_view ServiceName)
{
ZEN_UNUSED(ServiceName);
- const std::string DaemonName = GetDaemonName(ServiceName);
- const std::filesystem::path PListPath = GetPListPath(DaemonName);
+ const std::string DaemonName = GetDaemonName(ServiceName);
+ const std::filesystem::path PListPath = GetPListPath(DaemonName);
- std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl bootstrap system ") + PListPath.string());
+ std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl bootstrap system ") + PListPath.string());
if (Res.first != 0)
{
return MakeErrorCode(Res.first);
}
-
+
return {};
}
@@ -871,15 +866,15 @@ StopService(std::string_view ServiceName)
{
ZEN_UNUSED(ServiceName);
- const std::string DaemonName = GetDaemonName(ServiceName);
- const std::filesystem::path PListPath = GetPListPath(DaemonName);
+ const std::string DaemonName = GetDaemonName(ServiceName);
+ const std::filesystem::path PListPath = GetPListPath(DaemonName);
- std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl bootout system ") + PListPath.string());
+ std::pair<int, std::string> Res = ExecuteProgram(std::string("launchctl bootout system ") + PListPath.string());
if (Res.first != 0)
{
return MakeErrorCode(Res.first);
}
-
+
return {};
}