Add server source, originally in monado

This commit is contained in:
Guillaume Meunier
2022-11-12 23:59:28 +01:00
parent 07c0c09c80
commit 294c4f81ba
53 changed files with 10808 additions and 12 deletions
+36
View File
@@ -0,0 +1,36 @@
#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";
}