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>
This commit is contained in:
Pavel Šimerda 2014-11-18 18:12:16 +01:00
parent 3aede80152
commit 4805be2ed2

View file

@ -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;
}
out:
free (tmp_resolv_conf_realpath);
free (resolv_conf_realpath);
return *error ? FALSE : TRUE;
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;
}
return TRUE;
}
static void