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/zen/cmds/service_cmd.cpp | 6 ++++- src/zenutil/service.cpp | 55 ++++++++++++++++++-------------------------- 2 files changed, 28 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/zen/cmds/service_cmd.cpp b/src/zen/cmds/service_cmd.cpp index 8be76de7c..c7df6c640 100644 --- a/src/zen/cmds/service_cmd.cpp +++ b/src/zen/cmds/service_cmd.cpp @@ -102,7 +102,11 @@ namespace { } } -#else // ZEN_PLATFORM_WINDOWS +#elif ZEN_PLATFORM_MAC + + bool IsElevated() { return true; } // Mac service mode commands can run without elevation, so we lie a little bit here + +#else bool IsElevated() { return geteuid() == 0; } 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 566afb9f4a59c030d6f1235d1555fbe5968ea1b3 Mon Sep 17 00:00:00 2001 From: Liam Mitchell Date: Thu, 15 Jan 2026 17:13:53 -0800 Subject: Rename IsElevated to RequiresElevation --- src/zen/cmds/service_cmd.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/zen/cmds/service_cmd.cpp b/src/zen/cmds/service_cmd.cpp index c7df6c640..aa6bfb436 100644 --- a/src/zen/cmds/service_cmd.cpp +++ b/src/zen/cmds/service_cmd.cpp @@ -37,9 +37,9 @@ namespace zen { namespace { #if ZEN_PLATFORM_WINDOWS - BOOL IsElevated() + BOOL RequiresElevation() { - BOOL fRet = FALSE; + BOOL fRet = TRUE; HANDLE hToken = NULL; if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { @@ -47,7 +47,7 @@ namespace { DWORD cbSize = sizeof(TOKEN_ELEVATION); if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) { - fRet = Elevation.TokenIsElevated; + fRet = !Elevation.TokenIsElevated; } } if (hToken) @@ -104,11 +104,11 @@ namespace { #elif ZEN_PLATFORM_MAC - bool IsElevated() { return true; } // Mac service mode commands can run without elevation, so we lie a little bit here + bool RequiresElevation() { return false; } // Mac service mode commands can run without elevation #else - bool IsElevated() { return geteuid() == 0; } + bool RequiresElevation() { return geteuid() != 0; } #endif // ZEN_PLATFORM_WINDOWS @@ -348,7 +348,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) if (SubOption == &m_InstallOptions) { - if (!IsElevated()) + if (RequiresElevation()) { RunElevated(m_AllowElevation); return; @@ -557,7 +557,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) throw std::runtime_error(fmt::format("Service '{}' is running, stop before uninstalling", m_ServiceName)); } - if (!IsElevated()) + if (RequiresElevation()) { RunElevated(m_AllowElevation); return; @@ -589,7 +589,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - if (!IsElevated()) + if (RequiresElevation()) { RunElevated(m_AllowElevation); return; @@ -621,7 +621,7 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - if (!IsElevated()) + if (RequiresElevation()) { RunElevated(m_AllowElevation); 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') 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