Skip to content

Commit

Permalink
hostname: Fallback to transient hostname
Browse files Browse the repository at this point in the history
After pretty hostname, and static hostname, also support transient
hostname as a last resort before 'BlueZ X.XX'.

This happens on Fedora's Workstation installation as it calls
"hostnamectl set-hostname" on startup. In Fedora Silverblue, the default
hostname is set as fedora in /etc/os-release.

In both cases, we should fall back to that transient hostname, as bad as
it could be.

Note that the transient hostname needs to be monitored through the
kernel directly, as explained in:
https://www.freedesktop.org/software/systemd/man/org.freedesktop.hostname1.html
  • Loading branch information
hadess authored and Vudentz committed Nov 16, 2022
1 parent e515f4b commit e2b2b16
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions plugins/hostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/utsname.h>

#include "lib/bluetooth.h"
#include "lib/sdp.h"
Expand Down Expand Up @@ -44,8 +46,10 @@
static uint8_t major_class = MAJOR_CLASS_MISCELLANEOUS;
static uint8_t minor_class = MINOR_CLASS_UNCATEGORIZED;

static char *pretty_hostname = NULL;
static char *static_hostname = NULL;
static char *pretty_hostname = NULL;
static char *static_hostname = NULL;
static char *transient_hostname = NULL;
static guint hostname_id = 0;

/*
* Fallback to static hostname only if empty pretty hostname was already
Expand All @@ -60,6 +64,10 @@ static const char *get_hostname(void)
if (static_hostname &&
g_str_equal(static_hostname, "") == FALSE)
return static_hostname;

if (transient_hostname &&
g_str_equal(transient_hostname, "") == FALSE)
return transient_hostname;
}

return NULL;
Expand Down Expand Up @@ -181,6 +189,32 @@ static void property_changed(GDBusProxy *proxy, const char *name,
}
}

static void read_transient_hostname(void)
{
struct utsname u;

if (uname(&u) != 0) {
g_free(transient_hostname);
transient_hostname = NULL;
DBG("failed to read transient hostname");
return;
}

g_free(transient_hostname);
transient_hostname = g_strdup(u.nodename);

DBG("read transient hostname: '%s'", transient_hostname);
}

static gboolean hostname_cb(GIOChannel *io, GIOCondition cond,
gpointer user_data)
{
DBG("transient hostname changed");
read_transient_hostname();
adapter_foreach(update_class, NULL);
return TRUE;
}

static int hostname_probe(struct btd_adapter *adapter)
{
DBG("");
Expand Down Expand Up @@ -261,9 +295,11 @@ static GDBusProxy *hostname_proxy = NULL;
static int hostname_init(void)
{
DBusConnection *conn = btd_get_dbus_connection();
int fd;
int err;

read_dmi_fallback();
read_transient_hostname();

hostname_client = g_dbus_client_new(conn, "org.freedesktop.hostname1",
"/org/freedesktop/hostname1");
Expand All @@ -289,6 +325,17 @@ static int hostname_init(void)
hostname_client = NULL;
}

fd = open("/proc/sys/kernel/hostname", O_RDONLY);
if (fd < 0) {
error("open(/proc/sys/kernel/hostname): %s (%d)",
strerror(errno), errno);
} else {
GIOChannel *io = g_io_channel_unix_new(fd);

hostname_id = g_io_add_watch(io, G_IO_ERR, hostname_cb, NULL);
g_io_channel_unref(io);
}

return err;
}

Expand All @@ -306,8 +353,14 @@ static void hostname_exit(void)
hostname_client = NULL;
}

if (hostname_id != 0) {
g_source_remove(hostname_id);
hostname_id = 0;
}

g_free(pretty_hostname);
g_free(static_hostname);
g_free(transient_hostname);
}

BLUETOOTH_PLUGIN_DEFINE(hostname, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
Expand Down

0 comments on commit e2b2b16

Please sign in to comment.