]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Network: modify dnsmasq commandline build function to allow testing
authorMichal Novotny <minovotn@redhat.com>
Fri, 24 Jun 2011 10:04:38 +0000 (12:04 +0200)
committerLaine Stump <laine@laine.org>
Fri, 24 Jun 2011 20:15:17 +0000 (16:15 -0400)
The dnsmasq commandline was being built as a part of running
dnsmasq. This patch puts the commandline build into a separate
function (and exports it as a private API) making it possible to build
a dnsmasq commandline without executing it, so that we can write a
test program to verify that the proper commandlines are being created.

Signed-off-by: Michal Novotny <minovotn@redhat.com>
src/Makefile.am
src/libvirt_network.syms [new file with mode: 0644]
src/network/bridge_driver.c
src/network/bridge_driver.h

index e32740d8cc5963991a1e5a0c8aa7d094c661ba00..15a207e79f32c72cded7f528345d51fdefe4ff4f 100644 (file)
@@ -1087,6 +1087,10 @@ if WITH_XENXS
 USED_SYM_FILES += libvirt_xenxs.syms
 endif
 
+if WITH_NETWORK
+USED_SYM_FILES += libvirt_network.syms
+endif
+
 EXTRA_DIST += \
   libvirt_public.syms          \
   libvirt_private.syms         \
@@ -1097,7 +1101,8 @@ EXTRA_DIST += \
   libvirt_daemon.syms          \
   libvirt_nwfilter.syms        \
   libvirt_vmx.syms             \
-  libvirt_xenxs.syms
+  libvirt_xenxs.syms           \
+  libvirt_network.syms
 
 GENERATED_SYM_FILES = libvirt.syms libvirt.def libvirt_qemu.def
 
diff --git a/src/libvirt_network.syms b/src/libvirt_network.syms
new file mode 100644 (file)
index 0000000..6be5e45
--- /dev/null
@@ -0,0 +1,6 @@
+#
+# Network-specific symbols
+#
+
+# bridge_driver.h
+networkBuildDhcpDaemonCommandLine;
index dc143dbe7706b277f3b45b0a6277f90b91ff66ed..e3a1225ae04e3f58bc2474aad84b57ceb79a4474 100644 (file)
@@ -433,13 +433,20 @@ networkShutdown(void) {
 }
 
 
-static int
+static dnsmasqContext*
 networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef,
-                            dnsmasqContext *dctx,
+                            char *name,
                             bool force)
 {
     unsigned int i;
 
+    dnsmasqContext *dctx = dnsmasqContextNew(name,
+                                             DNSMASQ_STATE_DIR);
+    if (dctx == NULL) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
     if (! force && virFileExists(dctx->hostsfile->path))
         return 0;
 
@@ -450,9 +457,14 @@ networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef,
     }
 
     if (dnsmasqSave(dctx) < 0)
-        return -1;
+        goto cleanup;
 
-    return 0;
+    return dctx;
+
+cleanup:
+    dnsmasqContextFree(dctx);
+
+    return NULL;
 }
 
 
@@ -465,6 +477,7 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
     int nbleases = 0;
     int ii;
     virNetworkIpDefPtr tmpipdef;
+    dnsmasqContext *dctx = NULL;
 
     /*
      * NB, be careful about syntax for dnsmasq options in long format.
@@ -494,7 +507,8 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
     if (network->def->domain)
         virCommandAddArgList(cmd, "--domain", network->def->domain, NULL);
 
-    virCommandAddArgPair(cmd, "--pid-file", pidfile);
+    if (pidfile)
+        virCommandAddArgPair(cmd, "--pid-file", pidfile);
 
     /* *no* conf file */
     virCommandAddArg(cmd, "--conf-file=");
@@ -590,18 +604,11 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
         if (ipdef->nranges || ipdef->nhosts)
             virCommandAddArg(cmd, "--dhcp-no-override");
 
-        if (ipdef->nhosts > 0) {
-            dnsmasqContext *dctx = dnsmasqContextNew(network->def->name,
-                                                     DNSMASQ_STATE_DIR);
-            if (dctx == NULL) {
-                virReportOOMError();
-                goto cleanup;
-            }
+            if ((dctx = networkSaveDnsmasqHostsfile(ipdef, network->def->name, false))) {
+                if (dctx->hostsfile->nhosts)
+                    virCommandAddArgPair(cmd, "--dhcp-hostsfile",
+                                         dctx->hostsfile->path);
 
-            if (networkSaveDnsmasqHostsfile(ipdef, dctx, false) == 0) {
-                virCommandAddArgPair(cmd, "--dhcp-hostsfile",
-                                     dctx->hostsfile->path);
-            }
             dnsmasqContextFree(dctx);
         }
 
@@ -631,12 +638,12 @@ cleanup:
     return ret;
 }
 
-static int
-networkStartDhcpDaemon(virNetworkObjPtr network)
+int
+networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network, virCommandPtr *cmdout,
+                                  char *pidfile)
 {
     virCommandPtr cmd = NULL;
-    char *pidfile = NULL;
-    int ret = -1, err, ii;
+    int ret = -1, ii;
     virNetworkIpDefPtr ipdef;
 
     network->dnsmasqPid = -1;
@@ -661,6 +668,28 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
     if (!virNetworkDefGetIpByIndex(network->def, AF_UNSPEC, 0))
         return 0;
 
+    cmd = virCommandNew(DNSMASQ);
+    if (networkBuildDnsmasqArgv(network, ipdef, pidfile, cmd) < 0) {
+        goto cleanup;
+    }
+
+    if (cmdout)
+        *cmdout = cmd;
+    ret = 0;
+cleanup:
+    if (ret < 0)
+        virCommandFree(cmd);
+    return ret;
+}
+
+static int
+networkStartDhcpDaemon(virNetworkObjPtr network)
+{
+    virCommandPtr cmd = NULL;
+    char *pidfile = NULL;
+    int ret = -1;
+    int err;
+
     if ((err = virFileMakePath(NETWORK_PID_DIR)) != 0) {
         virReportSystemError(err,
                              _("cannot create directory %s"),
@@ -686,10 +715,9 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
         goto cleanup;
     }
 
-    cmd = virCommandNew(DNSMASQ);
-    if (networkBuildDnsmasqArgv(network, ipdef, pidfile, cmd) < 0) {
+    ret = networkBuildDhcpDaemonCommandLine(network,&cmd, pidfile);
+    if (ret<  0)
         goto cleanup;
-    }
 
     if (virCommandRun(cmd, NULL) < 0)
         goto cleanup;
@@ -2208,11 +2236,9 @@ static virNetworkPtr networkDefine(virConnectPtr conn, const char *xml) {
         }
     }
     if (ipv4def) {
-        dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
+        dnsmasqContext* dctx = networkSaveDnsmasqHostsfile(ipv4def, network->def->name, true);
         if (dctx == NULL)
             goto cleanup;
-
-        networkSaveDnsmasqHostsfile(ipv4def, dctx, true);
         dnsmasqContextFree(dctx);
     }
 
index 32d2ae71d98999bf8268d8b8496d07ea54ce02e4..8d82b677132fd8505c9d63c0891468f891de279e 100644 (file)
 # include <config.h>
 
 # include "internal.h"
+# include "network_conf.h"
+# include "command.h"
 
 int networkRegister(void);
+int networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network, virCommandPtr *cmdout, char *pidfile);
 
 #endif /* __VIR_NETWORK__DRIVER_H */