aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil
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/zenutil
parentdisplayname and description are windows-only properties (diff)
downloadzen-6aeec009d348a6bef2923f6eebb9997dfd363cdf.tar.xz
zen-6aeec009d348a6bef2923f6eebb9997dfd363cdf.zip
clang format
Diffstat (limited to 'src/zenutil')
-rw-r--r--src/zenutil/include/zenutil/service.h6
-rw-r--r--src/zenutil/service.cpp119
2 files changed, 60 insertions, 65 deletions
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 {};
}