diff options
Diffstat (limited to 'src/nm-policy.c')
-rw-r--r-- | src/nm-policy.c | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/src/nm-policy.c b/src/nm-policy.c index 05f7edf4db..e6abbc5428 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -120,9 +120,32 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) #define FALLBACK_HOSTNAME4 "localhost.localdomain" -static gboolean -set_system_hostname (const char *new_hostname, const char *msg) +static void settings_set_hostname_cb (char *hostname, gboolean result) { + int ret = 0; + + if (!result) { + ret = sethostname (hostname, strlen (hostname)); + if (ret != 0) { + int errsv = errno; + + nm_log_warn (LOGD_DNS, "couldn't set the system hostname to '%s': (%d) %s", + hostname, errsv, strerror (errsv)); + if (errsv == EPERM) + nm_log_warn (LOGD_DNS, "You should use hostnamed when systemd hardening is in effect!"); + } + } + + if (!ret) + nm_dispatcher_call (DISPATCHER_ACTION_HOSTNAME, NULL, NULL, NULL, NULL, NULL, NULL); + + g_free (hostname); +} + +static void +set_system_hostname (NMPolicy *self, const char *new_hostname, const char *msg) +{ + NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); char old_hostname[HOST_NAME_MAX + 1]; const char *name; int ret; @@ -140,23 +163,14 @@ set_system_hostname (const char *new_hostname, const char *msg) /* Don't set the hostname if it isn't actually changing */ if ( (new_hostname && !strcmp (old_hostname, new_hostname)) || (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME4))) - return FALSE; + return; } name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME4; - nm_log_info (LOGD_DNS, "Setting system hostname to '%s' (%s)", name, msg); - ret = sethostname (name, strlen (name)); - if (ret != 0) { - int errsv = errno; - - nm_log_warn (LOGD_DNS, "couldn't set the system hostname to '%s': (%d) %s", - name, errsv, strerror (errsv)); - if (errsv == EPERM) - nm_log_warn (LOGD_DNS, "You should use hostnamed when systemd hardening is in effect!"); - } + nm_settings_set_transient_hostname (priv->settings, g_strdup (name), settings_set_hostname_cb); - return (ret == 0); + return; } static void @@ -198,8 +212,7 @@ _set_hostname (NMPolicy *policy, nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname); - if (set_system_hostname (priv->cur_hostname, msg)) - nm_dispatcher_call (DISPATCHER_ACTION_HOSTNAME, NULL, NULL, NULL, NULL, NULL, NULL); + set_system_hostname (policy, priv->cur_hostname, msg); } static void |