Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp667077rwb; Wed, 9 Nov 2022 07:19:39 -0800 (PST) X-Google-Smtp-Source: AMsMyM7bbsiYhlAv7YXzgFoG+ztfeX6jIYHFD0ErA45mQxgss6uwrOQKw6y0hCGXsOy7AkVv5Riz X-Received: by 2002:a17:906:9bd2:b0:78c:3042:6919 with SMTP id de18-20020a1709069bd200b0078c30426919mr1170115ejc.377.1668007179426; Wed, 09 Nov 2022 07:19:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668007179; cv=none; d=google.com; s=arc-20160816; b=WHxj8XsJ8h3esV2d+4kyv0nf0NvOXVGbrY0OPuocjJTHxngIYVm52heiwOTGpwfdKk CE8yzCKilqfIJOAuBIfYD0MiwEf73KNyze3vHvnwS5evWd5J4HkFJbOVDsOc6sH+D4QV QIUJVIIi9m82QohKIlAmnd1yVgGUOwtGrIQ/cqG5Xrs986EcR77vsSdioi0oewXGI9Mf EB8uQY9atlNfvCrPPXnkqL5kg/nnxRNgI2ez1o5NjaVip0cUeyuF6CclF7RdbI7bUW4a o7/o2N9vCb4QAKR0n/UzdPEEh3RraTy/61kmZQ3Jd2ucdlOcPNg8Bg26fIvwR805mbJA EoXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from; bh=qx+bbo2052A5e+HQ48vN+dnilmY/FQj3zWO+B5u8JKc=; b=YLW6jGVJiDWJXvvURjmY2aXZ437ik0G9gCZZTRrkGmeINmHbcbegvqXGSC3zjLlYo6 UpuFdPoRSIw5jhKmF57tLC52zPHn2UL4EFm6lblIic4pqi2yvtqqAK09vnArzlS1oYtR T2RwKHZgHv7Gb3iMIqkYg3+yziv9nNc1Bm8g0dnqb2H1Nh5kG7v//8nV5IYsfDrdtKC5 T1PcTYc73s0yQ6J9aetWAs/L8RqwihlzXJmJXG3DPqPcFHcgX0Avj63T1Zxa1LwnzmFb yOKiQq2zFNvSW7FdclUzq2R1IPv/EyieqwalUK3V+8YvqkhjJFNMViBmBEjl5+E3Rg41 +LWQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o21-20020a170906975500b007ae10525563si18550068ejy.991.2022.11.09.07.19.02; Wed, 09 Nov 2022 07:19:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231918AbiKIPSI (ORCPT + 99 others); Wed, 9 Nov 2022 10:18:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231922AbiKIPSH (ORCPT ); Wed, 9 Nov 2022 10:18:07 -0500 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F25841CFF5 for ; Wed, 9 Nov 2022 07:18:05 -0800 (PST) Received: (Authenticated sender: hadess@hadess.net) by mail.gandi.net (Postfix) with ESMTPSA id 9F2761C000D for ; Wed, 9 Nov 2022 15:18:04 +0000 (UTC) From: Bastien Nocera To: linux-bluetooth@vger.kernel.org Subject: [BlueZ 2/2] hostname: Fallback to transient hostname Date: Wed, 9 Nov 2022 16:17:56 +0100 Message-Id: <20221109151756.96673-2-hadess@hadess.net> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221109151756.96673-1-hadess@hadess.net> References: <20221109151756.96673-1-hadess@hadess.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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 --- plugins/hostname.c | 53 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/plugins/hostname.c b/plugins/hostname.c index 14b6450b5..a12afb282 100644 --- a/plugins/hostname.c +++ b/plugins/hostname.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "lib/bluetooth.h" #include "lib/sdp.h" @@ -44,8 +45,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 @@ -60,6 +63,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; @@ -181,6 +188,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(""); @@ -261,9 +294,11 @@ static GDBusProxy *hostname_proxy = NULL; static int hostname_init(void) { DBusConnection *conn = btd_get_dbus_connection(); + GIOChannel *hostname_io; int err; read_dmi_fallback(); + read_transient_hostname(); hostname_client = g_dbus_client_new(conn, "org.freedesktop.hostname1", "/org/freedesktop/hostname1"); @@ -289,6 +324,14 @@ static int hostname_init(void) hostname_client = NULL; } + hostname_io = g_io_channel_new_file("/proc/sys/kernel/hostname", "r", NULL); + if (hostname_io) { + hostname_id = g_io_add_watch(hostname_io, G_IO_ERR, hostname_cb, NULL); + g_io_channel_unref(hostname_io); + } else { + DBG("failed to open /proc/sys/kernel/hostname"); + } + return err; } @@ -306,8 +349,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, -- 2.37.3