From d71135111f366f191a23dee961a080c7ae925a0f Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Thu, 21 Dec 2023 20:40:37 +0100 Subject: [PATCH] Merge hostname-sd and hostname files --- common/wivrn_config.h.in | 2 ++ server/CMakeLists.txt | 4 +--- server/hostname-sd.cpp | 35 ---------------------------- server/hostname.cpp | 50 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 49 insertions(+), 42 deletions(-) delete mode 100644 server/hostname-sd.cpp diff --git a/common/wivrn_config.h.in b/common/wivrn_config.h.in index d145272d..b901f997 100644 --- a/common/wivrn_config.h.in +++ b/common/wivrn_config.h.in @@ -22,3 +22,5 @@ #cmakedefine WIVRN_USE_NVENC #cmakedefine WIVRN_USE_VAAPI #cmakedefine WIVRN_USE_X264 + +#cmakedefine WIVRN_USE_SYSTEMD diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index cdbd6ed1..bd32aa52 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -58,6 +58,7 @@ add_executable(wivrn-server main.cpp target_instance_wivrn.cpp avahi_publisher.cpp + hostname.cpp wivrn_ipc.cpp driver/audio_pulse.cpp @@ -101,10 +102,7 @@ if(WIVRN_USE_X264) endif() if(WIVRN_USE_SYSTEMD) - target_sources(wivrn-server PRIVATE hostname-sd.cpp) target_link_libraries(wivrn-server PRIVATE PkgConfig::SYSTEMD) -else() - target_sources(wivrn-server PRIVATE hostname.cpp) endif() include(CompileGLSL) diff --git a/server/hostname-sd.cpp b/server/hostname-sd.cpp deleted file mode 100644 index 040d2875..00000000 --- a/server/hostname-sd.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "hostname.h" -#include - -std::string hostname() -{ - sd_bus * bus; - if (sd_bus_default_system(&bus) < 0) - return ""; - - for (auto property: {"PrettyHostname", "StaticHostname", "Hostname"}) - { - char * hostname = nullptr; - sd_bus_error error = SD_BUS_ERROR_NULL; - - if (sd_bus_get_property_string(bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", property, &error, &hostname) < 0) - { - sd_bus_error_free(&error); - continue; - } - - sd_bus_error_free(&error); - if (hostname && strcmp(hostname, "")) - { - std::string s = hostname; - free(hostname); - sd_bus_unref(bus); - return s; - } - } - - sd_bus_unref(bus); - - return "Unknown"; -} - diff --git a/server/hostname.cpp b/server/hostname.cpp index 95e8d40f..faff0de0 100644 --- a/server/hostname.cpp +++ b/server/hostname.cpp @@ -1,13 +1,55 @@ #include "hostname.h" -#include -#include #include +#include + +#include "util/u_logging.h" +#include "wivrn_config.h" + +#ifdef WIVRN_USE_SYSTEMD +#include +#endif std::string hostname() { + +#ifdef WIVRN_USE_SYSTEMD + sd_bus * bus; + if (sd_bus_default_system(&bus) < 0) + { + U_LOG_W("Failed to connect to system bus"); + } + else + { + for (auto property: {"PrettyHostname", "StaticHostname", "Hostname"}) + { + char * hostname = nullptr; + sd_bus_error error = SD_BUS_ERROR_NULL; + + if (sd_bus_get_property_string(bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", property, &error, &hostname) < 0) + { + sd_bus_error_free(&error); + continue; + } + + sd_bus_error_free(&error); + if (hostname && strcmp(hostname, "")) + { + std::string s = hostname; + free(hostname); + sd_bus_unref(bus); + return s; + } + } + + sd_bus_unref(bus); + } +#endif + char buf[HOST_NAME_MAX]; - int code = gethostname(buf, HOST_NAME_MAX); + int code = gethostname(buf, sizeof(buf)); if (code == 0) return buf; - return "unknown"; + + U_LOG_W("Failed to get hostname"); + return "no-hostname"; }