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 <mihai@fufexan.net>
This commit is contained in:
Yaroslav 2024-04-20 22:50:07 +05:00 committed by GitHub
parent ea47e8c92a
commit a945346064
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 76 additions and 31 deletions

View file

@ -8,7 +8,7 @@
#include "debug/HyprCtl.hpp"
#include "debug/CrashReporter.hpp"
#ifdef USES_SYSTEMD
#include <systemd/sd-daemon.h> // for sd_notify
#include <helpers/SdDaemon.hpp> // for SdNotify
#endif
#include <ranges>
#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

58
src/helpers/SdDaemon.cpp Normal file
View file

@ -0,0 +1,58 @@
#include "SdDaemon.hpp"
#include <memory>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
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<int, decltype(cleanup)> 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;
}
}

6
src/helpers/SdDaemon.hpp Normal file
View file

@ -0,0 +1,6 @@
#pragma once
namespace Systemd {
int SdBooted(void);
int SdNotify(int unset_environment, const char* state);
}

View file

@ -20,7 +20,6 @@ executable('Hyprland', src,
xcb_dep,
backtrace_dep,
epoll_dep,
systemd_dep,
udis86,
dependency('pixman-1'),