From 609f8fce1ce61c461c5a84ff7cbf97e084f05be3 Mon Sep 17 00:00:00 2001 From: Liam Mitchell Date: Wed, 7 Jan 2026 18:00:16 -0800 Subject: Implement final changes required for daemon mode on Mac --- src/zenutil/service.cpp | 55 +++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) (limited to 'src/zenutil/service.cpp') diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp index 103fdaa2f..9aa866e2c 100644 --- a/src/zenutil/service.cpp +++ b/src/zenutil/service.cpp @@ -229,12 +229,13 @@ namespace { std::filesystem::path GetPListPath(const std::string& DaemonName) { - const std::filesystem::path PListFolder = "/Library/LaunchDaemons"; + char* HomeDir = getenv("HOME"); + std::filesystem::path PListFolder = HomeDir; + PListFolder /= "Library/LaunchAgents"; return PListFolder / (DaemonName + ".plist"); } - std::string BuildPlist(std::string_view ServiceName, - const std::filesystem::path& ExecutablePath, + std::string BuildPlist(const std::filesystem::path& ExecutablePath, std::string_view CommandLineOptions, std::string_view DaemonName, bool Debug) @@ -265,14 +266,8 @@ namespace { " RunAtLoad\n" " \n" " \n" - // " KeepAlive\n" - // " \n" - // " \n" - " StandardOutPath\n" - " /var/log/{}.log\n" - " \n" - " StandardErrorPath\n" - " /var/log/{}.err.log\n" + " KeepAlive\n" + " \n" " \n" " Debug\n" " <{}/>\n" @@ -282,22 +277,7 @@ namespace { DaemonName, ExecutablePath, ProgramArguments.ToView(), - ServiceName, - ServiceName, Debug ? "true"sv : "false"sv); - - // "Sockets" - // "" - // "Listeners" - // "" - // "SockServiceName" - // "{}" // Listen socket - // "SockType" - // "tcp" - // "SockFamily" - // "IPv4" - // "" - // "" } #endif // ZEN_PLATFORM_MAC @@ -752,9 +732,8 @@ 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 the default service user OK? const std::string DaemonName = GetDaemonName(ServiceName); - std::string PList = BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true); + std::string PList = BuildPlist(Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true); const std::filesystem::path PListPath = GetPListPath(DaemonName); ZEN_INFO("Writing launchd plist to {}", PListPath.string()); @@ -910,8 +889,14 @@ StartService(std::string_view ServiceName) { const std::string DaemonName = GetDaemonName(ServiceName); const std::filesystem::path PListPath = GetPListPath(DaemonName); + std::pair User = ExecuteProgram("id -u"); - std::pair Res = ExecuteProgram(fmt::format("launchctl bootstrap system {}", PListPath)); + if (User.first != 0) + { + return MakeErrorCode(User.first); + } + + std::pair Res = ExecuteProgram(fmt::format("launchctl bootstrap gui/{} {}", User.second, PListPath)); if (Res.first != 0) { return MakeErrorCode(Res.first); @@ -926,13 +911,19 @@ StopService(std::string_view ServiceName) const std::string DaemonName = GetDaemonName(ServiceName); const std::filesystem::path PListPath = GetPListPath(DaemonName); - /* - std::pair Res = ExecuteProgram(fmt::format("launchctl bootout system ", PListPath.)); + std::pair User = ExecuteProgram("id -u"); + + if (User.first != 0) + { + return MakeErrorCode(User.first); + } + + std::pair Res = ExecuteProgram(fmt::format("launchctl bootout gui/{} {}", User.second, PListPath)); if (Res.first != 0) { return MakeErrorCode(Res.first); } - */ + return {}; } -- cgit v1.2.3 From ad58dee523116748ef1f8f62b3f2dcf42b5dd31a Mon Sep 17 00:00:00 2001 From: Liam Mitchell Date: Thu, 15 Jan 2026 17:19:45 -0800 Subject: Run clang-format on service.cpp --- src/zenutil/service.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/zenutil/service.cpp') diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp index 9aa866e2c..f2a925e61 100644 --- a/src/zenutil/service.cpp +++ b/src/zenutil/service.cpp @@ -229,7 +229,7 @@ namespace { std::filesystem::path GetPListPath(const std::string& DaemonName) { - char* HomeDir = getenv("HOME"); + char* HomeDir = getenv("HOME"); std::filesystem::path PListFolder = HomeDir; PListFolder /= "Library/LaunchAgents"; return PListFolder / (DaemonName + ".plist"); @@ -889,7 +889,7 @@ StartService(std::string_view ServiceName) { const std::string DaemonName = GetDaemonName(ServiceName); const std::filesystem::path PListPath = GetPListPath(DaemonName); - std::pair User = ExecuteProgram("id -u"); + std::pair User = ExecuteProgram("id -u"); if (User.first != 0) { @@ -924,7 +924,6 @@ StopService(std::string_view ServiceName) return MakeErrorCode(Res.first); } - return {}; } -- cgit v1.2.3