From db14c1b167d9ff8f45a18a818a836f8f84e24037 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 17 Aug 2016 18:40:17 +0200 Subject: [PATCH] policy: always try to update kernel hostname Even if we know that the new hostname being set is equal to the cached old one, the user may have manually changed the kernel hostname in the meanwhile. For example: # hostname host123 # hostname localhost # nmcli connection up eth1 # (now NM receives 'host123' from DHCP, but # believes it's already set and doesn't update it) # hostname localhost Let's always try to update the kernel (transient) hostname, unless it is really already set (as returned by gethostname()). https://bugzilla.redhat.com/show_bug.cgi?id=1356015 (cherry picked from commit 51b2cef04f7e929f012ae49a6c6d94e60bc68c16) --- src/nm-policy.c | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index eefbf1c7e..5fa46259c 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -180,38 +180,35 @@ _set_hostname (NMPolicy *self, if (new_hostname) g_clear_object (&priv->lookup_addr); - /* Don't change the hostname or update DNS this is the first time we're - * trying to change the hostname, and it's not actually changing. - */ if ( priv->orig_hostname && (priv->hostname_changed == FALSE) - && g_strcmp0 (priv->orig_hostname, new_hostname) == 0) - return; + && g_strcmp0 (priv->orig_hostname, new_hostname) == 0) { + /* Don't change the hostname or update DNS this is the first time we're + * trying to change the hostname, and it's not actually changing. + */ + } else if (g_strcmp0 (priv->cur_hostname, new_hostname) == 0) { + /* Don't change the hostname or update DNS if the hostname isn't actually + * going to change. + */ + } else { + g_free (priv->cur_hostname); + priv->cur_hostname = g_strdup (new_hostname); + priv->hostname_changed = TRUE; - /* Don't change the hostname or update DNS if the hostname isn't actually - * going to change. - */ - if (g_strcmp0 (priv->cur_hostname, new_hostname) == 0) - return; - - g_free (priv->cur_hostname); - priv->cur_hostname = g_strdup (new_hostname); - priv->hostname_changed = TRUE; - - /* Notify the DNS manager of the hostname change so that the domain part, if - * present, can be added to the search list. - */ - nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname); + /* Notify the DNS manager of the hostname change so that the domain part, if + * present, can be added to the search list. + */ + nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname); + } /* Finally, set kernel hostname */ - - if (!priv->cur_hostname) + if (!new_hostname) name = FALLBACK_HOSTNAME4; - else if (!priv->cur_hostname[0]) { + else if (!new_hostname[0]) { g_warn_if_reached (); name = FALLBACK_HOSTNAME4; } else - name = priv->cur_hostname; + name = new_hostname; old_hostname[HOST_NAME_MAX] = '\0'; errno = 0;