diff --git a/CMakeLists.txt b/CMakeLists.txt index b5e45d7b..8c206b7a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,20 +192,8 @@ endif() if(NO_SYSTEMD) message(STATUS "SYSTEMD support is disabled...") else() - message(STATUS "SYSTEMD support is requested (NO_SYSTEMD not defined) checking deps...") - check_include_file("systemd/sd-daemon.h" SYSTEMDH) - if(SYSTEMDH) - pkg_check_modules(LIBSYSTEMD libsystemd) - if (LIBSYSTEMD_FOUND) - add_compile_definitions(USES_SYSTEMD) - target_link_libraries(Hyprland "${LIBSYSTEMD_LIBRARIES}") - message(STATUS "Systemd found") - else() - message(WARNING "Systemd support requested but systemd libraries were not found") - endif() - else() - message(WARNING "Systemd support requested but systemd headers were not found") - endif() + message(STATUS "SYSTEMD support is requested (NO_SYSTEMD not defined)...") + add_compile_definitions(USES_SYSTEMD) endif() set(CPACK_PROJECT_NAME ${PROJECT_NAME}) diff --git a/meson.build b/meson.build index dd73cb92..40883073 100644 --- a/meson.build +++ b/meson.build @@ -52,14 +52,9 @@ endif backtrace_dep = cpp_compiler.find_library('execinfo', required: false) epoll_dep = dependency('epoll-shim', required: false) # timerfd on BSDs -systemd_dep = dependency('libsystemd', required: get_option('systemd')) if get_option('systemd').enabled() - if systemd_dep.found() - add_project_arguments('-DUSES_SYSTEMD', language: 'cpp') - else - error('Cannot enable systemd in Hyprland: libsystemd was not found') - endif + add_project_arguments('-DUSES_SYSTEMD', language: 'cpp') endif if get_option('legacy_renderer').enabled() diff --git a/nix/patches/meson-build.patch b/nix/patches/meson-build.patch index 5827e5c3..eb1d3f9a 100644 --- a/nix/patches/meson-build.patch +++ b/nix/patches/meson-build.patch @@ -1,5 +1,5 @@ diff --git a/meson.build b/meson.build -index 1d2c7f9f..c5ef4e67 100644 +index 40883073..d8f2e536 100644 --- a/meson.build +++ b/meson.build @@ -33,20 +33,7 @@ if cpp_compiler.check_header('execinfo.h') @@ -24,7 +24,7 @@ index 1d2c7f9f..c5ef4e67 100644 add_project_arguments('-DNO_XWAYLAND', language: 'cpp') endif -@@ -69,8 +56,6 @@ if get_option('buildtype') == 'debug' +@@ -65,8 +52,6 @@ if get_option('buildtype') == 'debug' add_project_arguments('-DHYPRLAND_DEBUG', language: 'cpp') endif @@ -34,7 +34,7 @@ index 1d2c7f9f..c5ef4e67 100644 headers = globber.stdout().strip().split('\n') foreach file : headers diff --git a/src/meson.build b/src/meson.build -index 45701f5f..3505cefe 100644 +index 15c69552..327aa4fb 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,7 +9,7 @@ executable('Hyprland', src, @@ -46,7 +46,7 @@ index 45701f5f..3505cefe 100644 dependency('cairo'), dependency('hyprcursor'), dependency('hyprlang', version: '>= 0.3.2'), -@@ -17,11 +17,11 @@ executable('Hyprland', src, +@@ -17,10 +17,10 @@ executable('Hyprland', src, dependency('egl'), dependency('xkbcommon'), dependency('libinput'), @@ -54,7 +54,6 @@ index 45701f5f..3505cefe 100644 + dependency('xcb', required: get_option('xwayland')), backtrace_dep, epoll_dep, - systemd_dep, - udis86, + dependency('udis86'), 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'),