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 <minovotn@redhat.com>
This commit is contained in:
Michal Novotny 2011-06-24 12:04:38 +02:00 committed by Laine Stump
parent 5dd986dbd7
commit 89ae9849f7
4 changed files with 67 additions and 27 deletions

View File

@ -1087,6 +1087,10 @@ if WITH_XENXS
USED_SYM_FILES += libvirt_xenxs.syms USED_SYM_FILES += libvirt_xenxs.syms
endif endif
if WITH_NETWORK
USED_SYM_FILES += libvirt_network.syms
endif
EXTRA_DIST += \ EXTRA_DIST += \
libvirt_public.syms \ libvirt_public.syms \
libvirt_private.syms \ libvirt_private.syms \
@ -1097,7 +1101,8 @@ EXTRA_DIST += \
libvirt_daemon.syms \ libvirt_daemon.syms \
libvirt_nwfilter.syms \ libvirt_nwfilter.syms \
libvirt_vmx.syms \ libvirt_vmx.syms \
libvirt_xenxs.syms libvirt_xenxs.syms \
libvirt_network.syms
GENERATED_SYM_FILES = libvirt.syms libvirt.def libvirt_qemu.def GENERATED_SYM_FILES = libvirt.syms libvirt.def libvirt_qemu.def

6
src/libvirt_network.syms Normal file
View File

@ -0,0 +1,6 @@
#
# Network-specific symbols
#
# bridge_driver.h
networkBuildDhcpDaemonCommandLine;

View File

@ -433,13 +433,20 @@ networkShutdown(void) {
} }
static int static dnsmasqContext*
networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef, networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef,
dnsmasqContext *dctx, char *name,
bool force) bool force)
{ {
unsigned int i; unsigned int i;
dnsmasqContext *dctx = dnsmasqContextNew(name,
DNSMASQ_STATE_DIR);
if (dctx == NULL) {
virReportOOMError();
goto cleanup;
}
if (! force && virFileExists(dctx->hostsfile->path)) if (! force && virFileExists(dctx->hostsfile->path))
return 0; return 0;
@ -450,9 +457,14 @@ networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef,
} }
if (dnsmasqSave(dctx) < 0) 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 nbleases = 0;
int ii; int ii;
virNetworkIpDefPtr tmpipdef; virNetworkIpDefPtr tmpipdef;
dnsmasqContext *dctx = NULL;
/* /*
* NB, be careful about syntax for dnsmasq options in long format. * NB, be careful about syntax for dnsmasq options in long format.
@ -494,6 +507,7 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
if (network->def->domain) if (network->def->domain)
virCommandAddArgList(cmd, "--domain", network->def->domain, NULL); virCommandAddArgList(cmd, "--domain", network->def->domain, NULL);
if (pidfile)
virCommandAddArgPair(cmd, "--pid-file", pidfile); virCommandAddArgPair(cmd, "--pid-file", pidfile);
/* *no* conf file */ /* *no* conf file */
@ -590,18 +604,11 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
if (ipdef->nranges || ipdef->nhosts) if (ipdef->nranges || ipdef->nhosts)
virCommandAddArg(cmd, "--dhcp-no-override"); virCommandAddArg(cmd, "--dhcp-no-override");
if (ipdef->nhosts > 0) { if ((dctx = networkSaveDnsmasqHostsfile(ipdef, network->def->name, false))) {
dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, if (dctx->hostsfile->nhosts)
DNSMASQ_STATE_DIR);
if (dctx == NULL) {
virReportOOMError();
goto cleanup;
}
if (networkSaveDnsmasqHostsfile(ipdef, dctx, false) == 0) {
virCommandAddArgPair(cmd, "--dhcp-hostsfile", virCommandAddArgPair(cmd, "--dhcp-hostsfile",
dctx->hostsfile->path); dctx->hostsfile->path);
}
dnsmasqContextFree(dctx); dnsmasqContextFree(dctx);
} }
@ -631,12 +638,12 @@ cleanup:
return ret; return ret;
} }
static int int
networkStartDhcpDaemon(virNetworkObjPtr network) networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network, virCommandPtr *cmdout,
char *pidfile)
{ {
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
char *pidfile = NULL; int ret = -1, ii;
int ret = -1, err, ii;
virNetworkIpDefPtr ipdef; virNetworkIpDefPtr ipdef;
network->dnsmasqPid = -1; network->dnsmasqPid = -1;
@ -661,6 +668,28 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
if (!virNetworkDefGetIpByIndex(network->def, AF_UNSPEC, 0)) if (!virNetworkDefGetIpByIndex(network->def, AF_UNSPEC, 0))
return 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) { if ((err = virFileMakePath(NETWORK_PID_DIR)) != 0) {
virReportSystemError(err, virReportSystemError(err,
_("cannot create directory %s"), _("cannot create directory %s"),
@ -686,10 +715,9 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
goto cleanup; goto cleanup;
} }
cmd = virCommandNew(DNSMASQ); ret = networkBuildDhcpDaemonCommandLine(network,&cmd, pidfile);
if (networkBuildDnsmasqArgv(network, ipdef, pidfile, cmd) < 0) { if (ret< 0)
goto cleanup; goto cleanup;
}
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
@ -2208,11 +2236,9 @@ static virNetworkPtr networkDefine(virConnectPtr conn, const char *xml) {
} }
} }
if (ipv4def) { if (ipv4def) {
dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR); dnsmasqContext* dctx = networkSaveDnsmasqHostsfile(ipv4def, network->def->name, true);
if (dctx == NULL) if (dctx == NULL)
goto cleanup; goto cleanup;
networkSaveDnsmasqHostsfile(ipv4def, dctx, true);
dnsmasqContextFree(dctx); dnsmasqContextFree(dctx);
} }

View File

@ -28,7 +28,10 @@
# include <config.h> # include <config.h>
# include "internal.h" # include "internal.h"
# include "network_conf.h"
# include "command.h"
int networkRegister(void); int networkRegister(void);
int networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network, virCommandPtr *cmdout, char *pidfile);
#endif /* __VIR_NETWORK__DRIVER_H */ #endif /* __VIR_NETWORK__DRIVER_H */