From a94534606412ec30620f68751382cea5b700ba8d Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 20 Apr 2024 22:50:07 +0500 Subject: core: remove libsystemd dependency (#5660) * remove libsystemd dependency as per Lennart Poettering's advice: https://github.com/systemd/systemd/issues/32028#issuecomment-2031366922 * fix naming for systemd helper functions * rename vars according to code style * Nix: update meson patch --------- Co-authored-by: Mihai Fufezan --- src/Compositor.cpp | 10 ++++----- src/helpers/SdDaemon.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/helpers/SdDaemon.hpp | 6 +++++ src/meson.build | 1 - 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/helpers/SdDaemon.cpp create mode 100644 src/helpers/SdDaemon.hpp (limited to 'src') diff --git a/src/Compositor.cpp b/src/Compositor.cpp index bc229d32..edf7c25d 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -8,7 +8,7 @@ #include "debug/HyprCtl.hpp" #include "debug/CrashReporter.hpp" #ifdef USES_SYSTEMD -#include // for sd_notify +#include // for SdNotify #endif #include #include "helpers/VarList.hpp" @@ -385,8 +385,8 @@ void CCompositor::cleanup() { Debug::shuttingDown = true; #ifdef USES_SYSTEMD - if (sd_booted() > 0 && !envEnabled("HYPRLAND_NO_SD_NOTIFY")) - sd_notify(0, "STOPPING=1"); + if (Systemd::SdBooted() > 0 && !envEnabled("HYPRLAND_NO_SD_NOTIFY")) + Systemd::SdNotify(0, "STOPPING=1"); #endif // unload all remaining plugins while the compositor is @@ -615,10 +615,10 @@ void CCompositor::startCompositor() { g_pHyprRenderer->setCursorFromName("left_ptr"); #ifdef USES_SYSTEMD - if (sd_booted() > 0) { + if (Systemd::SdBooted() > 0) { // tell systemd that we are ready so it can start other bond, following, related units if (!envEnabled("HYPRLAND_NO_SD_NOTIFY")) - sd_notify(0, "READY=1"); + Systemd::SdNotify(0, "READY=1"); } else Debug::log(LOG, "systemd integration is baked in but system itself is not booted à la systemd!"); #endif diff --git a/src/helpers/SdDaemon.cpp b/src/helpers/SdDaemon.cpp new file mode 100644 index 00000000..0835637a --- /dev/null +++ b/src/helpers/SdDaemon.cpp @@ -0,0 +1,58 @@ +#include "SdDaemon.hpp" + +#include + +#include +#include +#include +#include + +namespace Systemd { + int SdBooted(void) { + if (!faccessat(AT_FDCWD, "/run/systemd/system/", F_OK, AT_SYMLINK_NOFOLLOW)) + return true; + + if (errno == ENOENT) + return false; + + return -errno; + } + + int SdNotify(int unsetEnvironment, const char* state) { + int fd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (fd == -1) + return -errno; + + constexpr char envVar[] = "NOTIFY_SOCKET"; + + auto cleanup = [unsetEnvironment, envVar](int* fd) { + if (unsetEnvironment) + unsetenv(envVar); + close(*fd); + }; + std::unique_ptr fdCleaup(&fd, cleanup); + + const char* addr = getenv(envVar); + if (!addr) + return 0; + + // address length must be at most this; see man 7 unix + size_t addrLen = strnlen(addr, 107); + + struct sockaddr_un unixAddr; + unixAddr.sun_family = AF_UNIX; + strncpy(unixAddr.sun_path, addr, addrLen); + if (unixAddr.sun_path[0] == '@') + unixAddr.sun_path[0] = '\0'; + + if (!connect(fd, (const sockaddr*)&unixAddr, sizeof(struct sockaddr_un))) + return 1; + + // arbitrary value which seems to be enough for s-d messages + size_t stateLen = strnlen(state, 128); + if (write(fd, state, stateLen) >= 0) + return 1; + + return -errno; + } +} diff --git a/src/helpers/SdDaemon.hpp b/src/helpers/SdDaemon.hpp new file mode 100644 index 00000000..7b2b5de1 --- /dev/null +++ b/src/helpers/SdDaemon.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace Systemd { + int SdBooted(void); + int SdNotify(int unset_environment, const char* state); +} diff --git a/src/meson.build b/src/meson.build index d490cddb..15c69552 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,7 +20,6 @@ executable('Hyprland', src, xcb_dep, backtrace_dep, epoll_dep, - systemd_dep, udis86, dependency('pixman-1'), -- cgit v1.2.3