diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2022-09-07 16:50:02 +0200 |
---|---|---|
committer | Fernando Fernandez Mancera <ffmancera@riseup.net> | 2022-12-16 17:45:47 +0100 |
commit | badd1aa0b07f957e778b650449d874634e8ff318 (patch) | |
tree | 772e3a07a618181d0197ac72ecf6780f78f7ae0d | |
parent | dc39b9a3648d18dfc170f4565a5fbcea356c3164 (diff) | |
download | NetworkManager-badd1aa0b07f957e778b650449d874634e8ff318.tar.gz |
core: wait for carrier before resolving hostname via DNS
If there is no carrier on a device, don't try to resolve the hostname
on it. Instead, subscribe to carrier change notifications and retry
again once carrier goes up.
https://bugzilla.redhat.com/show_bug.cgi?id=2118817
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1402
(cherry picked from commit e3cf5083fba8dd1a3a1df69069de2f7e7411dd5e)
(cherry picked from commit 1673e3f0518cff15dd19f871baecdf64bef48bc7)
(cherry picked from commit 69e66102ce63c5f8794c9a8d53d060d03e9eb78d)
(cherry picked from commit a8f8ca01fdf833fc82c3533796345ea7ae019b3b)
(cherry picked from commit bd951c53986a94c7d09f28163be40481b80d86da)
-rw-r--r-- | src/core/devices/nm-device.c | 7 | ||||
-rw-r--r-- | src/core/nm-policy.c | 29 |
2 files changed, 33 insertions, 3 deletions
diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 6fc39c858a..8df5db51ae 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -17892,6 +17892,13 @@ nm_device_get_hostname_from_dns_lookup(NMDevice *self, int addr_family, gboolean /* If the device is not supposed to have addresses, * return an immediate empty result.*/ if (!nm_device_get_applied_connection(self)) { + nm_clear_pointer(&priv->hostname_resolver_x[IS_IPv4], _hostname_resolver_free); + NM_SET_OUT(out_wait, FALSE); + return NULL; + } + + if (!priv->carrier) { + nm_clear_pointer(&priv->hostname_resolver_x[IS_IPv4], _hostname_resolver_free); NM_SET_OUT(out_wait, FALSE); return NULL; } diff --git a/src/core/nm-policy.c b/src/core/nm-policy.c index e147e5047c..d79723eebe 100644 --- a/src/core/nm-policy.c +++ b/src/core/nm-policy.c @@ -794,6 +794,20 @@ device_dns_lookup_done(NMDevice *device, gpointer user_data) } static void +device_carrier_changed(NMDevice *device, GParamSpec *pspec, gpointer user_data) +{ + NMPolicyPrivate *priv = user_data; + NMPolicy * self = _PRIV_TO_SELF(priv); + gs_free char * msg = NULL; + + if (nm_device_has_carrier(device)) { + g_signal_handlers_disconnect_by_func(device, device_carrier_changed, priv); + msg = g_strdup_printf("device '%s' got carrier", nm_device_get_iface(device)); + update_system_hostname(self, msg); + } +} + +static void update_system_hostname(NMPolicy *self, const char *msg) { NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE(self); @@ -877,6 +891,7 @@ update_system_hostname(NMPolicy *self, const char *msg) info = &g_array_index(infos, DeviceHostnameInfo, i); addr_family = info->IS_IPv4 ? AF_INET : AF_INET6; g_signal_handlers_disconnect_by_func(info->device, device_dns_lookup_done, self); + g_signal_handlers_disconnect_by_func(info->device, device_carrier_changed, priv); if (info->from_dhcp) { dhcp_config = nm_device_get_dhcp_config(info->device, addr_family); @@ -902,10 +917,18 @@ update_system_hostname(NMPolicy *self, const char *msg) if (priv->hostname_mode != NM_POLICY_HOSTNAME_MODE_DHCP) { if (info->from_dns) { - const char *result; - gboolean wait = FALSE; + const char *result = NULL; + gboolean wait = FALSE; - result = nm_device_get_hostname_from_dns_lookup(info->device, addr_family, &wait); + if (nm_device_has_carrier(info->device)) { + result = + nm_device_get_hostname_from_dns_lookup(info->device, addr_family, &wait); + } else { + g_signal_connect(info->device, + "notify::" NM_DEVICE_CARRIER, + G_CALLBACK(device_carrier_changed), + priv); + } if (result) { _set_hostname(self, result, "from address lookup"); return; |