From 6e86ad8e0fff2b48fd266836edc8e73b52f0871b Mon Sep 17 00:00:00 2001 From: "Patrick J. Volkerding" Date: Fri, 22 Apr 2016 20:30:56 -0500 Subject: [PATCH 1/3] settings: fix Slackware hostname setting https://mail.gnome.org/archives/networkmanager-list/2016-April/msg00075.html (cherry picked from commit 1a714ee5e68f855b816ee947d4c49cb25c8227a4) --- src/settings/nm-settings.c | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 4ad395a10b..18def2fe5d 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -504,6 +504,41 @@ read_hostname_gentoo (const char *path) } #endif +#if defined(HOSTNAME_PERSIST_SLACKWARE) +static gchar * +read_hostname_slackware (const char *path) +{ + gchar *contents = NULL, *result = NULL, *tmp; + gchar **all_lines = NULL; + guint line_num, i, j = 0; + + if (!g_file_get_contents (path, &contents, NULL, NULL)) + return NULL; + all_lines = g_strsplit (contents, "\n", 0); + line_num = g_strv_length (all_lines); + for (i = 0; i < line_num; i++) { + g_strstrip (all_lines[i]); + if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') + continue; + tmp = &all_lines[i][0]; + /* We only want up to the first '.' -- the rest of the */ + /* fqdn is defined in /etc/hosts */ + while (tmp[j] != '\0') { + if (tmp[j] == '.') { + tmp[j] = '\0'; + break; + } + j++; + } + result = g_shell_unquote (tmp, NULL); + break; + } + g_strfreev (all_lines); + g_free (contents); + return result; +} +#endif + #if defined(HOSTNAME_PERSIST_SUSE) static gboolean hostname_is_dynamic (void) @@ -551,6 +586,10 @@ nm_settings_get_hostname (NMSettings *self) hostname = read_hostname_gentoo (priv->hostname.file); #else +#if defined(HOSTNAME_PERSIST_SLACKWARE) + hostname = read_hostname_slackware (priv->hostname.file); +#else + #if defined(HOSTNAME_PERSIST_SUSE) if (priv->hostname.dhcp_monitor_id && hostname_is_dynamic ()) return NULL; @@ -559,6 +598,7 @@ nm_settings_get_hostname (NMSettings *self) g_strchomp (hostname); #endif /* HOSTNAME_PERSIST_GENTOO */ +#endif /* HOSTNAME_PERSIST_SLACKWARE */ out: if (hostname && !hostname[0]) { From eb4e6945657e96ad19a90228c87c286561c66ede Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 25 Apr 2016 15:03:31 +0200 Subject: [PATCH 2/3] settings: cleanup #if blocks for HOSTNAME_PERSIST_* (cherry picked from commit 79d85ca180c7510e9e6a07e7295d84f0151f0243) --- src/settings/nm-settings.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 18def2fe5d..a47ae6792a 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -112,6 +112,10 @@ EXPORT(nm_settings_connection_replace_and_commit) #define PLUGIN_MODULE_PATH "plugin-module-path" +#if (defined(HOSTNAME_PERSIST_SUSE) + defined(HOSTNAME_PERSIST_SLACKWARE) + defined(HOSTNAME_PERSIST_GENTOO)) > 1 +#error "Can only define one of HOSTNAME_PERSIST_*" +#endif + #if defined(HOSTNAME_PERSIST_SUSE) #define HOSTNAME_FILE HOSTNAME_FILE_UCASE_HOSTNAME #elif defined(HOSTNAME_PERSIST_SLACKWARE) @@ -526,7 +530,7 @@ read_hostname_slackware (const char *path) while (tmp[j] != '\0') { if (tmp[j] == '.') { tmp[j] = '\0'; - break; + break; } j++; } @@ -582,23 +586,19 @@ nm_settings_get_hostname (NMSettings *self) goto out; } -#if defined(HOSTNAME_PERSIST_GENTOO) - hostname = read_hostname_gentoo (priv->hostname.file); -#else - -#if defined(HOSTNAME_PERSIST_SLACKWARE) - hostname = read_hostname_slackware (priv->hostname.file); -#else - #if defined(HOSTNAME_PERSIST_SUSE) if (priv->hostname.dhcp_monitor_id && hostname_is_dynamic ()) return NULL; #endif + +#if defined(HOSTNAME_PERSIST_GENTOO) + hostname = read_hostname_gentoo (priv->hostname.file); +#elif defined(HOSTNAME_PERSIST_SLACKWARE) + hostname = read_hostname_slackware (priv->hostname.file); +#else if (g_file_get_contents (priv->hostname.file, &hostname, NULL, NULL)) g_strchomp (hostname); - -#endif /* HOSTNAME_PERSIST_GENTOO */ -#endif /* HOSTNAME_PERSIST_SLACKWARE */ +#endif out: if (hostname && !hostname[0]) { From 9f6dd5afb7dfd33195f311b6b3a07f0461c68fa0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 25 Apr 2016 15:13:18 +0200 Subject: [PATCH 3/3] settings: refactor read_hostname_*() (cherry picked from commit 316359d8b65030df2ac8b34be926bc752a96802c) --- src/settings/nm-settings.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index a47ae6792a..7475eb5fae 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -484,27 +484,25 @@ get_plugin (NMSettings *self, guint32 capability) static gchar * read_hostname_gentoo (const char *path) { - gchar *contents = NULL, *result = NULL, *tmp; - gchar **all_lines = NULL; - guint line_num, i; + gs_free char *contents = NULL; + gs_strfreev char **all_lines = NULL; + const char *tmp; + guint i; if (!g_file_get_contents (path, &contents, NULL, NULL)) return NULL; + all_lines = g_strsplit (contents, "\n", 0); - line_num = g_strv_length (all_lines); - for (i = 0; i < line_num; i++) { + for (i = 0; all_lines[i]; i++) { g_strstrip (all_lines[i]); if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') continue; if (g_str_has_prefix (all_lines[i], "hostname=")) { tmp = &all_lines[i][NM_STRLEN ("hostname=")]; - result = g_shell_unquote (tmp, NULL); - break; + return g_shell_unquote (tmp, NULL); } } - g_strfreev (all_lines); - g_free (contents); - return result; + return NULL; } #endif @@ -512,15 +510,16 @@ read_hostname_gentoo (const char *path) static gchar * read_hostname_slackware (const char *path) { - gchar *contents = NULL, *result = NULL, *tmp; - gchar **all_lines = NULL; - guint line_num, i, j = 0; + gs_free char *contents = NULL; + gs_strfreev char **all_lines = NULL; + char *tmp; + guint i, j = 0; if (!g_file_get_contents (path, &contents, NULL, NULL)) return NULL; + all_lines = g_strsplit (contents, "\n", 0); - line_num = g_strv_length (all_lines); - for (i = 0; i < line_num; i++) { + for (i = 0; all_lines[i]; i++) { g_strstrip (all_lines[i]); if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') continue; @@ -534,12 +533,9 @@ read_hostname_slackware (const char *path) } j++; } - result = g_shell_unquote (tmp, NULL); - break; + return g_shell_unquote (tmp, NULL); } - g_strfreev (all_lines); - g_free (contents); - return result; + return NULL; } #endif