summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Šimerda <psimerda@redhat.com>2014-11-18 18:12:16 +0100
committerPavel Šimerda <psimerda@redhat.com>2014-12-23 13:34:25 +0100
commit4805be2ed27b71a6099477d86dbc109adb41b819 (patch)
treee47e103be7e2d00125e30c91608a58ba740710b0
parent3aede801521ef7bff039e6e3f1b3c7b566b4338d (diff)
downloadNetworkManager-4805be2ed27b71a6099477d86dbc109adb41b819.tar.gz
dns-manager: make /etc/resolv.conf a symlink to /run/NetworkManager/resolv.conf.default
Related: * https://bugzilla.gnome.org/show_bug.cgi?id=732941 * https://bugzilla.redhat.com/show_bug.cgi?id=1116999 Acked-By: Thomas Haller <thaller@redhat.com> Acked-By: Dan Williams <dcbw@redhat.com>
-rw-r--r--src/dns-manager/nm-dns-manager.c111
1 files changed, 57 insertions, 54 deletions
diff --git a/src/dns-manager/nm-dns-manager.c b/src/dns-manager/nm-dns-manager.c
index 0d93d8bb41..f4de57773c 100644
--- a/src/dns-manager/nm-dns-manager.c
+++ b/src/dns-manager/nm-dns-manager.c
@@ -436,53 +436,27 @@ dispatch_resolvconf (char **searches,
}
#endif
+#define MY_RESOLV_CONF NMRUNDIR "/resolv.conf"
+#define MY_RESOLV_CONF_TMP MY_RESOLV_CONF ".tmp"
+#define RESOLV_CONF_TMP "/etc/.resolv.conf.NetworkManager"
+
static gboolean
update_resolv_conf (char **searches,
char **nameservers,
GError **error)
{
- char *tmp_resolv_conf;
- char *tmp_resolv_conf_realpath;
- char *resolv_conf_realpath;
FILE *f;
- int do_rename = 1;
- int old_errno = 0;
g_return_val_if_fail (error != NULL, FALSE);
- /* Find the real path of resolv.conf; it could be a symlink to something */
- resolv_conf_realpath = realpath (_PATH_RESCONF, NULL);
- if (!resolv_conf_realpath)
- resolv_conf_realpath = strdup (_PATH_RESCONF);
-
- /* Build up the real path for the temp resolv.conf that we're about to
- * write out.
- */
- tmp_resolv_conf = g_strdup_printf ("%s.tmp", resolv_conf_realpath);
- tmp_resolv_conf_realpath = realpath (tmp_resolv_conf, NULL);
- if (!tmp_resolv_conf_realpath)
- tmp_resolv_conf_realpath = strdup (tmp_resolv_conf);
- g_free (tmp_resolv_conf);
- tmp_resolv_conf = NULL;
-
- if ((f = fopen (tmp_resolv_conf_realpath, "w")) == NULL) {
- do_rename = 0;
- old_errno = errno;
- if ((f = fopen (_PATH_RESCONF, "w")) == NULL) {
- g_set_error (error,
- NM_MANAGER_ERROR,
- NM_MANAGER_ERROR_FAILED,
- "Could not open %s: %s\nCould not open %s: %s\n",
- tmp_resolv_conf_realpath,
- g_strerror (old_errno),
- _PATH_RESCONF,
- g_strerror (errno));
- goto out;
- }
- /* Update tmp_resolv_conf_realpath so the error message on fclose()
- * failure will be correct.
- */
- strcpy (tmp_resolv_conf_realpath, _PATH_RESCONF);
+ if ((f = fopen (MY_RESOLV_CONF_TMP, "w")) == NULL) {
+ g_set_error (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_FAILED,
+ "Could not open %s: %s\n",
+ MY_RESOLV_CONF_TMP,
+ g_strerror (errno));
+ return FALSE;
}
write_resolv_conf (f, searches, nameservers, error);
@@ -496,28 +470,57 @@ update_resolv_conf (char **searches,
NM_MANAGER_ERROR,
NM_MANAGER_ERROR_FAILED,
"Could not close %s: %s\n",
- tmp_resolv_conf_realpath,
+ MY_RESOLV_CONF_TMP,
g_strerror (errno));
}
}
- /* Don't rename the tempfile over top of the existing resolv.conf if there
- * was an error writing it out.
- */
- if (*error == NULL && do_rename) {
- if (rename (tmp_resolv_conf_realpath, resolv_conf_realpath) < 0) {
- g_set_error (error,
- NM_MANAGER_ERROR,
- NM_MANAGER_ERROR_FAILED,
- "Could not replace " _PATH_RESCONF ": %s\n",
- g_strerror (errno));
- }
+ if (*error)
+ return FALSE;
+
+ if (rename (MY_RESOLV_CONF_TMP, MY_RESOLV_CONF) < 0) {
+ g_set_error (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_FAILED,
+ "Could not replace %s: %s\n",
+ MY_RESOLV_CONF,
+ g_strerror (errno));
+ return FALSE;
+ }
+
+ if (unlink (RESOLV_CONF_TMP) == -1 && errno != ENOENT) {
+ g_set_error (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_FAILED,
+ "Could not unlink %s: %s\n",
+ RESOLV_CONF_TMP,
+ g_strerror (errno));
+ return FALSE;
+ }
+
+ if (symlink (MY_RESOLV_CONF, RESOLV_CONF_TMP) == -1) {
+ g_set_error (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_FAILED,
+ "Could not create symlink %s pointing to %s: %s\n",
+ RESOLV_CONF_TMP,
+ MY_RESOLV_CONF,
+ g_strerror (errno));
+ return FALSE;
+ }
+
+ if (rename (RESOLV_CONF_TMP, _PATH_RESCONF) == -1) {
+ g_set_error (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_FAILED,
+ "Could not rename %s to %s: %s\n",
+ RESOLV_CONF_TMP,
+ _PATH_RESCONF,
+ g_strerror (errno));
+ return FALSE;
}
-out:
- free (tmp_resolv_conf_realpath);
- free (resolv_conf_realpath);
- return *error ? FALSE : TRUE;
+ return TRUE;
}
static void