diff options
| author | Dan Engelbrecht <[email protected]> | 2025-01-09 15:13:38 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2025-01-09 15:13:38 +0100 |
| commit | cf4f59b6f7dc60703dea2470a38fa741cba80501 (patch) | |
| tree | 3a9ecefbc058a52efbeea38b2a73edb7b0281241 /src/zenutil/service.cpp | |
| parent | mac function signature fix (diff) | |
| download | zen-cf4f59b6f7dc60703dea2470a38fa741cba80501.tar.xz zen-cf4f59b6f7dc60703dea2470a38fa741cba80501.zip | |
partial macos implementation
Diffstat (limited to 'src/zenutil/service.cpp')
| -rw-r--r-- | src/zenutil/service.cpp | 71 |
1 files changed, 59 insertions, 12 deletions
diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp index 0bacc03f5..60db75f00 100644 --- a/src/zenutil/service.cpp +++ b/src/zenutil/service.cpp @@ -15,12 +15,16 @@ # include <unistd.h> # include <sys/stat.h> + +#include <xpc/xpc.h> #endif namespace zen { using namespace std::literals; namespace { +#if ZEN_PLATFORM_WINDOWS + bool SplitExecutableAndArgs(const std::wstring& ExeAndArgs, std::filesystem::path& OutExecutablePath, std::string& OutArguments) { if (ExeAndArgs.size()) @@ -58,11 +62,10 @@ namespace { } return false; } -} // namespace -#if ZEN_PLATFORM_MAC +#endif // ZEN_PLATFORM_WINDOWS -namespace { +#if ZEN_PLATFORM_MAC std::vector<std::string_view> SplitArguments(std::string_view Arguments) { bool IsQuote = false; @@ -238,9 +241,10 @@ namespace { // "</dict>" // "</dict>" } -} // namespace #endif // ZEN_PLATFORM_MAC +} // namespace + std::string_view ToString(ServiceLevel Level) @@ -926,6 +930,22 @@ static int CopyFile(std::filesystem::path source, std::filesystem::path dest) } # endif +std::filesystem::path GetDaemonBasePath(ServiceLevel Level) +{ + switch(Level) + { + case ServiceLevel::CurrentUser: + return GetUserHomeFolder() / "Library/LaunchAgents"; + case ServiceLevel::AllUsers: + return "/Library/LaunchAgents"; + case ServiceLevel::SystemService: + return "/Library/LaunchDaemon"; + default: + ZEN_ASSERT(false); + return {}; + } +} + std::error_code InstallService(std::string_view ServiceName, const ServiceSpec& Spec) { @@ -936,17 +956,37 @@ InstallService(std::string_view ServiceName, const ServiceSpec& Spec) // { // return MakeErrorCodeFromLastError(); // } + + // System: /Library/LaunchDaemons + // All users: /Library/LaunchAgents + // Current user: ~/Library/LaunchAgents + + std::string DaemonName = fmt::format("com.epicgames.unreal.{}", ServiceName); std::string PList = BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, Spec.DisplayName, Spec.Description, true); - std::filesystem::path PListPath = std::filesystem::path("/Users/dan.engelbrecht/Library/LaunchAgents") / (DaemonName + ".plist"); + + std::filesystem::path PListFolder = GetDaemonBasePath(Spec.ServiceLevel); + + std::filesystem::path PListPath = PListFolder / (DaemonName + ".plist"); ZEN_INFO("Writing launchd plist to {}", PListPath.string()); - zen::WriteFile(PListPath, IoBuffer(IoBuffer::Wrap, PList.data(), PList.size())); - ZEN_INFO("Changing permissions to 0555 for {}", PListPath.string()); - // if (chmod(PListPath.string().c_str(), 0555) == -1) - // { - // return MakeErrorCodeFromLastError(); - // } + try + { + zen::WriteFile(PListPath, IoBuffer(IoBuffer::Wrap, PList.data(), PList.size())); + } + catch(const std::system_error& Ex) + { + return MakeErrorCode(Ex.code().value()); + } + + if (Spec.ServiceLevel != ServiceLevel::CurrentUser) // ???? Correct? + { + ZEN_INFO("Changing permissions to 600 for {}", PListPath.string()); + if (chmod(PListPath.string().c_str(), 0600) == -1) + { + return MakeErrorCodeFromLastError(); + } + } return {}; } @@ -976,7 +1016,12 @@ std::error_code QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) { ZEN_UNUSED(ServiceName, OutInfo); - ZEN_NOT_IMPLEMENTED("QueryInstalledService"); +// ZEN_NOT_IMPLEMENTED("QueryInstalledService"); + //std::filesystem::path PListFolder = GetDaemonBasePath(Spec.ServiceLevel); + //sudo launchctl list + + OutInfo.Status = ServiceStatus::NotInstalled; + return {}; } @@ -985,6 +1030,7 @@ StartService(std::string_view ServiceName, ServiceLevel Level) { ZEN_UNUSED(ServiceName, Level); ZEN_NOT_IMPLEMENTED("StartService"); + // sudo launchctl bootstrap system /Library/LaunchDaemon/com.epicgames.unreal.ZenServer.plist return {}; } @@ -993,6 +1039,7 @@ StopService(std::string_view ServiceName, ServiceLevel Level) { ZEN_UNUSED(ServiceName, Level); ZEN_NOT_IMPLEMENTED("StopService"); + // sudo launchctl bootout system /Library/LaunchDaemon/com.epicgames.unreal.ZenServer.plist return {}; } |