}
-static dnsmasqContext*
-networkSaveDnsmasqHostsfile(virNetworkIpDefPtr ipdef,
- virNetworkDNSDefPtr dnsdef,
- char *name,
- bool force)
+static int
+networkBuildDnsmasqHostsfile(dnsmasqContext *dctx,
+ virNetworkIpDefPtr ipdef,
+ virNetworkDNSDefPtr dnsdef)
{
unsigned int i, j;
- dnsmasqContext *dctx = dnsmasqContextNew(name,
- DNSMASQ_STATE_DIR);
- if (dctx == NULL) {
- virReportOOMError();
- goto cleanup;
- }
-
- if (!(! force && virFileExists(dctx->hostsfile->path))) {
- for (i = 0; i < ipdef->nhosts; i++) {
- virNetworkDHCPHostDefPtr host = &(ipdef->hosts[i]);
- if ((host->mac) && VIR_SOCKET_HAS_ADDR(&host->ip))
- dnsmasqAddDhcpHost(dctx, host->mac, &host->ip, host->name);
- }
+ for (i = 0; i < ipdef->nhosts; i++) {
+ virNetworkDHCPHostDefPtr host = &(ipdef->hosts[i]);
+ if ((host->mac) && VIR_SOCKET_HAS_ADDR(&host->ip))
+ dnsmasqAddDhcpHost(dctx, host->mac, &host->ip, host->name);
}
if (dnsdef) {
}
}
- if (dnsmasqSave(dctx) < 0)
- goto cleanup;
-
- return dctx;
-
-cleanup:
- dnsmasqContextFree(dctx);
-
- return NULL;
+ return 0;
}
networkBuildDnsmasqArgv(virNetworkObjPtr network,
virNetworkIpDefPtr ipdef,
const char *pidfile,
- virCommandPtr cmd) {
+ virCommandPtr cmd,
+ dnsmasqContext *dctx)
+{
int r, ret = -1;
int nbleases = 0;
int ii;
virNetworkIpDefPtr tmpipdef;
- dnsmasqContext *dctx = NULL;
/*
* NB, be careful about syntax for dnsmasq options in long format.
if (network->def->domain)
virCommandAddArg(cmd, "--expand-hosts");
- if ((dctx = networkSaveDnsmasqHostsfile(ipdef, network->def->dns, network->def->name, false))) {
+ if (networkBuildDnsmasqHostsfile(dctx, ipdef, network->def->dns) >= 0) {
if (dctx->hostsfile->nhosts)
virCommandAddArgPair(cmd, "--dhcp-hostsfile",
dctx->hostsfile->path);
if (dctx->addnhostsfile->nhosts)
virCommandAddArgPair(cmd, "--addn-hosts",
dctx->addnhostsfile->path);
- dnsmasqContextFree(dctx);
}
if (ipdef->tftproot) {
int
networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network, virCommandPtr *cmdout,
- char *pidfile)
+ char *pidfile, dnsmasqContext *dctx)
{
virCommandPtr cmd = NULL;
int ret = -1, ii;
return 0;
cmd = virCommandNew(DNSMASQ);
- if (networkBuildDnsmasqArgv(network, ipdef, pidfile, cmd) < 0) {
+ if (networkBuildDnsmasqArgv(network, ipdef, pidfile, cmd, dctx) < 0) {
goto cleanup;
}
char *pidfile = NULL;
int ret = -1;
int err;
+ dnsmasqContext *dctx = NULL;
if ((err = virFileMakePath(NETWORK_PID_DIR)) != 0) {
virReportSystemError(err,
goto cleanup;
}
- ret = networkBuildDhcpDaemonCommandLine(network,&cmd, pidfile);
- if (ret< 0)
+ dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
+ if (dctx == NULL)
+ goto cleanup;
+
+ ret = networkBuildDhcpDaemonCommandLine(network, &cmd, pidfile, dctx);
+ if (ret < 0)
+ goto cleanup;
+
+ ret = dnsmasqSave(dctx);
+ if (ret < 0)
goto cleanup;
if (virCommandRun(cmd, NULL) < 0)
cleanup:
VIR_FREE(pidfile);
virCommandFree(cmd);
+ dnsmasqContextFree(dctx);
return ret;
}
virNetworkObjPtr network = NULL;
virNetworkPtr ret = NULL;
int ii;
+ dnsmasqContext* dctx = NULL;
networkDriverLock(driver);
}
}
if (ipv4def) {
- dnsmasqContext* dctx = networkSaveDnsmasqHostsfile(ipv4def, network->def->dns, network->def->name, true);
- if (dctx == NULL)
+ dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
+ if (dctx == NULL ||
+ networkBuildDnsmasqHostsfile(dctx, ipv4def, network->def->dns) < 0 ||
+ dnsmasqSave(dctx) < 0)
goto cleanup;
- dnsmasqContextFree(dctx);
}
VIR_INFO("Defining network '%s'", network->def->name);
cleanup:
virNetworkDefFree(def);
+ dnsmasqContextFree(dctx);
if (network)
virNetworkObjUnlock(network);
networkDriverUnlock(driver);
addnhostsNew(const char *name,
const char *config_dir)
{
- int err;
dnsmasqAddnHostsfile *addnhostsfile;
if (VIR_ALLOC(addnhostsfile) < 0) {
goto error;
}
- if ((err = virFileMakePath(config_dir))) {
- virReportSystemError(err, _("cannot create config directory '%s'"),
- config_dir);
- goto error;
- }
-
return addnhostsfile;
error:
hostsfileNew(const char *name,
const char *config_dir)
{
- int err;
dnsmasqHostsfile *hostsfile;
if (VIR_ALLOC(hostsfile) < 0) {
goto error;
}
- if ((err = virFileMakePath(config_dir))) {
- virReportSystemError(err, _("cannot create config directory '%s'"),
- config_dir);
- goto error;
- }
-
return hostsfile;
error:
return NULL;
}
+ if (!(ctx->config_dir = strdup(config_dir))) {
+ virReportOOMError();
+ goto error;
+ }
+
if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
goto error;
if (!(ctx->addnhostsfile = addnhostsNew(network_name, config_dir)))
if (!ctx)
return;
+ VIR_FREE(ctx->config_dir);
+
if (ctx->hostsfile)
hostsfileFree(ctx->hostsfile);
if (ctx->addnhostsfile)
int
dnsmasqSave(const dnsmasqContext *ctx)
{
+ int err;
int ret = 0;
+ if ((err = virFileMakePath(ctx->config_dir))) {
+ virReportSystemError(err, _("cannot create config directory '%s'"),
+ ctx->config_dir);
+ return -1;
+ }
+
if (ctx->hostsfile)
ret = hostsfileSave(ctx->hostsfile);
if (ret == 0) {