[ovs-dev,2/2] dns-resolve: Improve on handling of system DNS nameserver

Message ID 1541627074-16939-2-git-send-email-pkusunyifeng@gmail.com
State Accepted
Headers show
Series
  • [ovs-dev,1/2] dns-resolve: Stop dns resolving if no DNS server configured
Related show

Commit Message

Yifeng Sun Nov. 7, 2018, 9:44 p.m.
This patch enables OVS on windows to read system nameserver configuration.
In addition, a new environment variable OVS_RESOLV_CONF is introduced.
If set, it can be used as DNS server configuration file. This variable 
is supposed to be used for sandboxing other things. It is documented
accordingly.

Suggested-by: Ben Pfaff <blp@ovn.org>
Suggested-by: Mark Michelson <mmichels@redhat.com>
Signed-off-by: Yifeng Sun <pkusunyifeng@gmail.com>
---
 Documentation/intro/install/general.rst |  2 ++
 NEWS                                    |  2 ++
 lib/dns-resolve.c                       | 16 +++++++++++-----
 3 files changed, 15 insertions(+), 5 deletions(-)

Comments

Ben Pfaff Nov. 9, 2018, 9:21 p.m. | #1
On Wed, Nov 07, 2018 at 01:44:34PM -0800, Yifeng Sun wrote:
> This patch enables OVS on windows to read system nameserver configuration.
> In addition, a new environment variable OVS_RESOLV_CONF is introduced.
> If set, it can be used as DNS server configuration file. This variable 
> is supposed to be used for sandboxing other things. It is documented
> accordingly.
> 
> Suggested-by: Ben Pfaff <blp@ovn.org>
> Suggested-by: Mark Michelson <mmichels@redhat.com>
> Signed-off-by: Yifeng Sun <pkusunyifeng@gmail.com>

Thanks for the patches.  I applied both of these to master.  (They
didn't apply to branch-2.10.)

Patch

diff --git a/Documentation/intro/install/general.rst b/Documentation/intro/install/general.rst
index 537137ef58df..71dfeaefca27 100644
--- a/Documentation/intro/install/general.rst
+++ b/Documentation/intro/install/general.rst
@@ -97,6 +97,8 @@  need the following software:
   you want to enable ovs-vswitchd and other utilities to use DNS names when
   specifying OpenFlow and OVSDB remotes. If unbound library is already
   installed, then Open vSwitch will automatically build with support for it.
+  The environment variable OVS_RESOLV_CONF can be used to specify DNS server
+  configuration file (the default file on Linux is /etc/resolv.conf).
 
 On Linux, you may choose to compile the kernel module that comes with the Open
 vSwitch distribution or to use the kernel module built into the Linux kernel
diff --git a/NEWS b/NEWS
index fc8ab05def4a..8eba5a38fafc 100644
--- a/NEWS
+++ b/NEWS
@@ -16,6 +16,8 @@  Post-v2.10.0
    - ovs-vswitchd:
      * New configuration option "offload-rebalance", that enables dynamic
        rebalancing of offloaded flows.
+   - The environment variable OVS_RESOLV_CONF, if set, is now used
+     as the DNS server configuration file.
 
 v2.10.0 - 18 Aug 2018
 ---------------------
diff --git a/lib/dns-resolve.c b/lib/dns-resolve.c
index 7d735749e5a5..3e4e57b9379a 100644
--- a/lib/dns-resolve.c
+++ b/lib/dns-resolve.c
@@ -82,14 +82,21 @@  dns_resolve_init(bool is_daemon)
         return;
     }
 
-#ifdef __linux__
-    const char *filename = "/etc/resolv.conf";
+    const char *filename = getenv("OVS_RESOLV_CONF");
+    if (!filename) {
+#ifdef _WIN32
+        /* On Windows, NULL means to use the system default nameserver. */
+#else
+        filename = "/etc/resolv.conf";
+#endif
+    }
     struct stat s;
-    if (!stat(filename, &s) || errno != ENOENT) {
+    if (!filename || (!stat(filename, &s) || errno != ENOENT)) {
         int retval = ub_ctx_resolvconf(ub_ctx__, filename);
         if (retval != 0) {
             VLOG_WARN_RL(&rl, "Failed to read %s: %s",
-                         filename, ub_strerror(retval));
+                         filename ? filename : "system default nameserver",
+                         ub_strerror(retval));
             ub_ctx_delete(ub_ctx__);
             ub_ctx__ = NULL;
             return;
@@ -101,7 +108,6 @@  dns_resolve_init(bool is_daemon)
         ub_ctx__ = NULL;
         return;
     }
-#endif
 
     /* Handles '/etc/hosts' on Linux and 'WINDIR/etc/hosts' on Windows. */
     int retval = ub_ctx_hosts(ub_ctx__, NULL);