diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2014-03-31 15:04:16 +0200 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2014-03-31 16:56:31 +0200 |
commit | b9e18dd30763952a74d96d2a90f7c8e79b612077 (patch) | |
tree | 750a4aa6bfad3e2a2e3df52ae24e998a87f67e6f | |
parent | bd2a5e11477a66373386d3bb1ebba2d206f8c858 (diff) | |
download | NetworkManager-b9e18dd30763952a74d96d2a90f7c8e79b612077.tar.gz |
keyfile: read/write hostname from/to /etc/hostname instead of NM config file
plugin_get_hostname()
- reads hostname from /etc/hostname
- fallbacks to NetworkManager.conf
plugin_set_hostname()
- writes hostname to /etc/hostname
- removes hostname from the NetworkManager.conf (if present)
-rw-r--r-- | src/settings/plugins/keyfile/Makefile.am | 1 | ||||
-rw-r--r-- | src/settings/plugins/keyfile/plugin.c | 42 |
2 files changed, 31 insertions, 12 deletions
diff --git a/src/settings/plugins/keyfile/Makefile.am b/src/settings/plugins/keyfile/Makefile.am index c2e8fdaeab..a0d0e0f2de 100644 --- a/src/settings/plugins/keyfile/Makefile.am +++ b/src/settings/plugins/keyfile/Makefile.am @@ -6,6 +6,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/src \ -I$(top_srcdir)/src/config \ -I$(top_srcdir)/src/settings \ + -I$(top_srcdir)/src/platform \ -I$(top_srcdir)/include \ -I$(top_builddir)/include \ -I$(top_srcdir)/libnm-util \ diff --git a/src/settings/plugins/keyfile/plugin.c b/src/settings/plugins/keyfile/plugin.c index 3439f55d5e..5e801144ec 100644 --- a/src/settings/plugins/keyfile/plugin.c +++ b/src/settings/plugins/keyfile/plugin.c @@ -43,6 +43,7 @@ #include "writer.h" #include "common.h" #include "utils.h" +#include "NetworkManagerUtils.h" static char *plugin_get_hostname (SCPluginKeyfile *plugin); static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class); @@ -533,6 +534,8 @@ get_unmanaged_specs (NMSystemConfigInterface *config) return specs; } +#define HOSTNAME_FILE "/etc/hostname" + static char * plugin_get_hostname (SCPluginKeyfile *plugin) { @@ -541,6 +544,13 @@ plugin_get_hostname (SCPluginKeyfile *plugin) char *hostname = NULL; GError *error = NULL; + /* Get hostname from HOSTNAME_FILE */ + if (g_file_get_contents (HOSTNAME_FILE, &hostname, NULL, NULL)) { + g_strchomp (hostname); + return hostname; + } + + /* Fallback to NetworkManager.conf */ if (!priv->conf_file) return NULL; @@ -570,31 +580,39 @@ plugin_set_hostname (SCPluginKeyfile *plugin, const char *hostname) GError *error = NULL; char *data = NULL; gsize len; + char *hostname_eol; - if (!priv->conf_file) { - g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Error saving hostname: no config file"); - goto out; + /* Save hostname to /etc/hostname */ + hostname_eol = g_strdup_printf ("%s\n", hostname); + if (!nm_utils_file_set_contents (HOSTNAME_FILE, hostname_eol, -1, NULL)) { + PLUGIN_WARN (KEYFILE_PLUGIN_NAME, "Could not save hostname: failed to create/open " HOSTNAME_FILE); + g_free (hostname_eol); + return FALSE; } g_free (priv->hostname); priv->hostname = g_strdup (hostname); + if (!priv->conf_file) + goto out; + key_file = g_key_file_new (); if (!parse_key_file_allow_none (priv, key_file, &error)) goto out; - g_key_file_set_string (key_file, "keyfile", "hostname", hostname); + if (g_key_file_get_value (key_file, "keyfile", "hostname", NULL)) { + /* Remove hostname from configuration file */ + g_key_file_remove_key (key_file, "keyfile", "hostname", NULL); - data = g_key_file_to_data (key_file, &len, &error); - if (!data) - goto out; + data = g_key_file_to_data (key_file, &len, &error); + if (!data) + goto out; - if (!g_file_set_contents (priv->conf_file, data, len, &error)) { - g_prefix_error (&error, "Error saving hostname: "); - goto out; + if (!g_file_set_contents (priv->conf_file, data, len, &error)) { + g_prefix_error (&error, "Error in plugin_set_hostname(): "); + goto out; + } } - ret = TRUE; out: |