From: Michal Novotny Date: Fri, 24 Jun 2011 10:04:38 +0000 (+0200) Subject: Network: modify dnsmasq commandline build function to allow testing X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=89ae9849f7443b6842c9e60a4269df966b1204ed;p=people%2Fliuw%2Flibxenctrl-split%2Flibvirt.git Network: modify dnsmasq commandline build function to allow testing 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 --- diff --git a/src/Makefile.am b/src/Makefile.am index e32740d8c..15a207e79 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 000000000..6be5e45c9 --- /dev/null +++ b/src/libvirt_network.syms @@ -0,0 +1,6 @@ +# +# Network-specific symbols +# + +# bridge_driver.h +networkBuildDhcpDaemonCommandLine; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index dc143dbe7..e3a1225ae 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -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); } diff --git a/src/network/bridge_driver.h b/src/network/bridge_driver.h index 32d2ae71d..8d82b6771 100644 --- a/src/network/bridge_driver.h +++ b/src/network/bridge_driver.h @@ -28,7 +28,10 @@ # include # 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 */