[PATCH v2] settings: Resolve path if hostname is a sym-link



If the hostname file is a symbolic link, follow it to find where the
real file is located, otherwise g_file_set_contents will attempt to
replace the link with a plain file.
---
 src/settings/nm-settings.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c
index f6f8c37..fc2eb29 100644
--- a/src/settings/nm-settings.c
+++ b/src/settings/nm-settings.c
@@ -1532,11 +1532,11 @@ write_hostname (NMSettingsPrivate *priv, const char *hostname)
        char *hostname_eol;
        gboolean ret;
        gs_free_error GError *error = NULL;
-       const char *file = priv->hostname.file;
+       char *file = g_strdup(priv->hostname.file), *link_path;
        gs_unref_variant GVariant *var = NULL;
+       struct stat file_stat = { .st_mode = 0 };
 #if HAVE_SELINUX
        security_context_t se_ctx_prev = NULL, se_ctx = NULL;
-       struct stat file_stat = { .st_mode = 0 };
        mode_t st_mode = 0;
 #endif
 
@@ -1554,6 +1554,16 @@ write_hostname (NMSettingsPrivate *priv, const char *hostname)
                return !error;
        }
 
+       /* If the hostname file is a symbolic link, follow it to find where the
+        * real file is located, otherwise g_file_set_contents will attempt to
+        * replace the link with a plain file.
+        */
+       if (lstat (file, &file_stat) == 0 && S_ISLNK (file_stat.st_mode) &&
+               (link_path = g_file_read_link (file, NULL))) {
+               g_free (file);
+               file = link_path;
+       }
+
 #if HAVE_SELINUX
        /* Get default context for hostname file and set it for fscreate */
        if (stat (file, &file_stat) == 0)
-- 
1.9.1



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]