}
-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;
}
if (dnsmasqSave(dctx) < 0)
- return -1;
+ goto cleanup;
- return 0;
+ return dctx;
+
+cleanup:
+ dnsmasqContextFree(dctx);
+
+ return NULL;
}
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)
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=");
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);
}
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;
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"),
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;
}
}
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);
}