diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2015-03-23 17:36:34 +0100 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2015-03-23 22:05:13 +0100 |
commit | c4849fbf96a622309e1419ac4443324e206d9544 (patch) | |
tree | a5121884a466e26947bb8ab0d926a7495fd55c78 | |
parent | ae8f508938752c47f39cb378e21672df1f74b6cf (diff) | |
download | NetworkManager-c4849fbf96a622309e1419ac4443324e206d9544.tar.gz |
settings: add hostnamed support
When the systemd-hostnamed daemon is available, use it to read and
change the hostname.
Based on 'danw/wip/hostnamed' branch by Dan Winship <danw@gnome.org>
-rw-r--r-- | src/settings/nm-settings.c | 101 |
1 files changed, 95 insertions, 6 deletions
diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 60e0676bbe..a043e9b225 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -98,6 +98,10 @@ EXPORT(nm_settings_connection_replace_settings) EXPORT(nm_settings_connection_replace_and_commit) /* END LINKER CRACKROCK */ +#define HOSTNAMED_SERVICE_NAME "org.freedesktop.hostname1" +#define HOSTNAMED_SERVICE_PATH "/org/freedesktop/hostname1" +#define HOSTNAMED_SERVICE_INTERFACE "org.freedesktop.hostname1" + #define HOSTNAME_FILE_DEFAULT "/etc/hostname" #define HOSTNAME_FILE_SUSE "/etc/HOSTNAME" #define HOSTNAME_FILE_IFNET "/etc/conf.d/hostname" @@ -165,6 +169,7 @@ typedef struct { gboolean startup_complete; + GDBusProxy *hostnamed_proxy; char *hostname; char *hostname_file; gboolean hostname_file_ifnet; @@ -592,7 +597,9 @@ nm_settings_get_hostname (NMSettings *self) NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); char *hostname = NULL; - if (priv->dhcp_monitor_id && hostname_is_dynamic ()) { + if (priv->hostnamed_proxy) { + return strdup (priv->hostname); + } else if (priv->dhcp_monitor_id && hostname_is_dynamic ()) { return NULL; } else if (priv->hostname_file_ifnet) { return read_hostname_ifnet (priv->hostname_file); @@ -1548,13 +1555,37 @@ write_hostname_to_file (const char *file, const char *hostname, gboolean is_ifne static gboolean set_hostname (NMSettingsPrivate *priv, const char *hostname) { + GError *error = NULL; + GVariant *var; gboolean ret; - ret = write_hostname_to_file (priv->hostname_file, hostname, - priv->hostname_file_ifnet); + if (priv->hostnamed_proxy) { + var = g_dbus_proxy_call_sync (priv->hostnamed_proxy, + "SetHostname", + g_variant_new ("(sb)", + hostname, + FALSE), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (var) + g_variant_unref (var); + + if (error) { + nm_log_warn (LOGD_SETTINGS, "Could not set hostname: %s", error->message); + g_error_free (error); + ret = FALSE; + } else { + ret = TRUE; + } + } else { + ret = write_hostname_to_file (priv->hostname_file, hostname, + priv->hostname_file_ifnet); - if (sysconfig_hostname_clear) - sysconfig_hostname_clear (); + if (sysconfig_hostname_clear) + sysconfig_hostname_clear (); + } return ret; } @@ -2010,6 +2041,32 @@ nm_settings_get_startup_complete (NMSettings *self) /***************************************************************/ static void +hostnamed_properties_changed (GDBusProxy *proxy, + GVariant *changed_properties, + char **invalidated_properties, + gpointer user_data) +{ + NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (user_data); + GVariant *v_hostname; + const char *hostname; + + v_hostname = g_dbus_proxy_get_cached_property (priv->hostnamed_proxy, "StaticHostname"); + if (!v_hostname) + return; + + hostname = g_variant_get_string (v_hostname, NULL); + + if (g_strcmp0 (priv->hostname, hostname) != 0) { + nm_log_info (LOGD_SETTINGS, "hostname changed from '%s' to '%s'", + priv->hostname, hostname); + priv->hostname = g_strdup (hostname); + g_object_notify (G_OBJECT (user_data), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME); + } + + g_variant_unref (v_hostname); +} + +static void setup_hostname_file_monitors (NMSettings *self) { NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); @@ -2071,6 +2128,28 @@ setup_hostname_file_monitors (NMSettings *self) } } +static void +on_proxy_acquired (GObject *object, GAsyncResult *res, NMSettings *self) +{ + NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); + GError *error = NULL; + + priv->hostnamed_proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + if (!priv->hostnamed_proxy) { + nm_log_warn (LOGD_SETTINGS, "Failed to acquire hostnamed proxy: %s", error->message); + g_clear_error (&error); + + setup_hostname_file_monitors(self); + return; + } + + nm_log_info (LOGD_SETTINGS, "hostnamed proxy acquired"); + + g_signal_connect (priv->hostnamed_proxy, "g-properties-changed", + G_CALLBACK (hostnamed_properties_changed), self); + hostnamed_properties_changed (priv->hostnamed_proxy, NULL, NULL, self); +} + NMSettings * nm_settings_new (GError **error) { @@ -2092,8 +2171,11 @@ nm_settings_new (GError **error) load_connections (self); check_startup_complete (self); - setup_hostname_file_monitors (self); + g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, 0, NULL, + HOSTNAMED_SERVICE_NAME, HOSTNAMED_SERVICE_PATH, + HOSTNAMED_SERVICE_INTERFACE, NULL, + (GAsyncReadyCallback) on_proxy_acquired, self); nm_dbus_manager_register_object (priv->dbus_mgr, NM_DBUS_PATH_SETTINGS, self); return self; } @@ -2138,6 +2220,13 @@ dispose (GObject *object) g_object_unref (priv->agent_mgr); + if (priv->hostnamed_proxy) { + g_signal_handlers_disconnect_by_func (priv->hostnamed_proxy, + G_CALLBACK (hostnamed_properties_changed), + self); + g_clear_object(&priv->hostnamed_proxy); + } + if (priv->hostname_monitor) { if (priv->hostname_monitor_id) g_signal_handler_disconnect (priv->hostname_monitor, priv->hostname_monitor_id); |