Merge hostname-sd and hostname files

This commit is contained in:
Patrick Nicolas
2023-12-21 20:40:37 +01:00
parent 5c447dd6d7
commit d71135111f
4 changed files with 49 additions and 42 deletions
+2
View File
@@ -22,3 +22,5 @@
#cmakedefine WIVRN_USE_NVENC
#cmakedefine WIVRN_USE_VAAPI
#cmakedefine WIVRN_USE_X264
#cmakedefine WIVRN_USE_SYSTEMD
+1 -3
View File
@@ -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)
-35
View File
@@ -1,35 +0,0 @@
#include "hostname.h"
#include <systemd/sd-bus.h>
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";
}
+46 -4
View File
@@ -1,13 +1,55 @@
#include "hostname.h"
#include <string>
#include <unistd.h>
#include <limits.h>
#include <unistd.h>
#include "util/u_logging.h"
#include "wivrn_config.h"
#ifdef WIVRN_USE_SYSTEMD
#include <systemd/sd-bus.h>
#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";
}