---
Hi,

it is very annoying when connman clears /etc/resolv.conf and
removes users own settings there. This patch remembers user
settings (if dnsproxy is not in use i.e., -r option is being
used) and does not remove them unnecessarily.

Jukka


 src/resolver.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/src/resolver.c b/src/resolver.c
index 7d15c14..22d2f98 100644
--- a/src/resolver.c
+++ b/src/resolver.c
@@ -24,6 +24,7 @@
 #endif
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -75,10 +76,15 @@ static void resolvfile_remove_entries(GList *entries)
 static int resolvfile_export(void)
 {
        GList *list;
-       GString *content;
+       GString *content = NULL;
+       GString *existing_content = NULL;
        int fd, err;
        unsigned int count;
        mode_t old_umask;
+       FILE *fp;
+       char *line = NULL;
+       size_t len = 0;
+       ssize_t read;
 
        content = g_string_new("# Generated by Connection Manager\n");
 
@@ -104,7 +110,7 @@ static int resolvfile_export(void)
        }
 
        if (count)
-               g_string_append_printf(content, "\n");
+               g_string_append_printf(content, " # by Connection Manager\n");
 
        for (count = 0, list = g_list_last(resolvfile_list);
                                                list && (count < MAXNS);
@@ -114,11 +120,36 @@ static int resolvfile_export(void)
                if (!entry->server)
                        continue;
 
-               g_string_append_printf(content, "nameserver %s\n",
+               g_string_append_printf(content, "nameserver %s # by Connection 
Manager\n",
                                                                entry->server);
                count++;
        }
 
+       /*
+        * Read existing entries from resolv.conf and do not overwrite them
+        * when dnsproxy is not in use.
+        */
+       if (dnsproxy_enabled == FALSE) {
+               fp = fopen("/etc/resolv.conf", "r");
+               if (fp) {
+                       while ((read = getline(&line, &len, fp)) != -1) {
+                               if (g_str_has_suffix(line,
+                                               "by Connection Manager\n"))
+                                       continue;
+
+                               if (existing_content == NULL)
+                                       existing_content = g_string_new(line);
+                               else
+                                       g_string_append(existing_content,
+                                                       line);
+                       }
+                       if (line)
+                               free(line);
+
+                       fclose(fp);
+               }
+       }
+
        old_umask = umask(022);
 
        fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
@@ -135,14 +166,25 @@ static int resolvfile_export(void)
 
        err = 0;
 
-       if (write(fd, content->str, content->len) < 0)
-               err = -errno;
+       /* Do not save the plain header */
+       if (dnsproxy_enabled || g_strcmp0(content->str,
+                               "# Generated by Connection Manager\n"))
+               if (write(fd, content->str, content->len) < 0)
+                       err = -errno;
+
+       if (existing_content && write(fd, existing_content->str,
+                                       existing_content->len) < 0)
+               connman_warn("Cannot copy old DNS entries back to "
+                               "/etc/resolv.conf");
 
 failed:
        close(fd);
 
 done:
        g_string_free(content, TRUE);
+       if (existing_content)
+               g_string_free(existing_content, TRUE);
+
        umask(old_umask);
 
        return err;
@@ -467,13 +509,14 @@ int __connman_resolver_init(connman_bool_t dnsproxy)
        if (dnsproxy == FALSE)
                return 0;
 
+       dnsproxy_enabled = TRUE;
+
        if (__connman_dnsproxy_init() < 0) {
                /* Fall back to resolv.conf */
+               dnsproxy_enabled = FALSE;
                return 0;
        }
 
-       dnsproxy_enabled = TRUE;
-
        return 0;
 }
 
@@ -496,5 +539,8 @@ void __connman_resolver_cleanup(void)
                        free_entry(slist->data);
                g_slist_free(entry_list);
                entry_list = NULL;
+
+               /* Clear connman entries from /etc/resolv.conf */
+               resolvfile_export();
        }
 }
-- 
1.7.1

_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to