libvirt/src/util/virdnsmasq.c

750 lines
17 KiB
C
Raw Normal View History

/*
* virdnsmasq.c: Helper APIs for managing dnsmasq
*
* Copyright (C) 2007-2013 Red Hat, Inc.
* Copyright (C) 2010 Satoru SATOH <satoru.satoh@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Based on iptables.c
*/
#include <config.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include "internal.h"
#include "datatypes.h"
#include "virbitmap.h"
#include "virdnsmasq.h"
2012-12-13 17:44:57 +00:00
#include "virutil.h"
#include "vircommand.h"
2012-12-12 18:06:53 +00:00
#include "viralloc.h"
#include "virerror.h"
2012-12-12 17:59:27 +00:00
#include "virlog.h"
#include "virfile.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NETWORK
VIR_LOG_INIT("util.dnsmasq");
#define DNSMASQ_HOSTSFILE_SUFFIX "hostsfile"
#define DNSMASQ_ADDNHOSTSFILE_SUFFIX "addnhosts"
#define DNSMASQ_MIN_MAJOR 2
#define DNSMASQ_MIN_MINOR 67
static void
dhcphostFreeContent(dnsmasqDhcpHost *host)
{
g_free(host->host);
}
static void
addnhostFreeContent(dnsmasqAddnHost *host)
{
size_t i;
for (i = 0; i < host->nhostnames; i++)
g_free(host->hostnames[i]);
g_free(host->hostnames);
g_free(host->ip);
}
static void
addnhostsFree(dnsmasqAddnHostsfile *addnhostsfile)
{
size_t i;
if (addnhostsfile->hosts) {
for (i = 0; i < addnhostsfile->nhosts; i++)
addnhostFreeContent(&addnhostsfile->hosts[i]);
g_free(addnhostsfile->hosts);
addnhostsfile->nhosts = 0;
}
g_free(addnhostsfile->path);
g_free(addnhostsfile);
}
static int
addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
virSocketAddr *ip,
const char *name)
{
char *ipstr = NULL;
int idx = -1;
size_t i;
if (!(ipstr = virSocketAddrFormat(ip)))
return -1;
for (i = 0; i < addnhostsfile->nhosts; i++) {
if (STREQ((const char *)addnhostsfile->hosts[i].ip, (const char *)ipstr)) {
idx = i;
break;
}
}
if (idx < 0) {
VIR_REALLOC_N(addnhostsfile->hosts, addnhostsfile->nhosts + 1);
idx = addnhostsfile->nhosts;
addnhostsfile->hosts[idx].hostnames = g_new0(char *, 1);
addnhostsfile->hosts[idx].ip = g_strdup(ipstr);
addnhostsfile->hosts[idx].nhostnames = 0;
addnhostsfile->nhosts++;
}
VIR_REALLOC_N(addnhostsfile->hosts[idx].hostnames, addnhostsfile->hosts[idx].nhostnames + 1);
addnhostsfile->hosts[idx].hostnames[addnhostsfile->hosts[idx].nhostnames] = g_strdup(name);
VIR_FREE(ipstr);
addnhostsfile->hosts[idx].nhostnames++;
return 0;
}
static dnsmasqAddnHostsfile *
addnhostsNew(const char *name,
const char *config_dir)
{
dnsmasqAddnHostsfile *addnhostsfile;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
addnhostsfile = g_new0(dnsmasqAddnHostsfile, 1);
addnhostsfile->hosts = NULL;
addnhostsfile->nhosts = 0;
virBufferAsprintf(&buf, "%s", config_dir);
virBufferEscapeString(&buf, "/%s", name);
virBufferAsprintf(&buf, ".%s", DNSMASQ_ADDNHOSTSFILE_SUFFIX);
if (!(addnhostsfile->path = virBufferContentAndReset(&buf)))
goto error;
return addnhostsfile;
error:
addnhostsFree(addnhostsfile);
return NULL;
}
static int
addnhostsWrite(const char *path,
dnsmasqAddnHost *hosts,
unsigned int nhosts)
{
g_autofree char *tmp = NULL;
FILE *f;
bool istmp = true;
size_t i, j;
int rc = 0;
network: always create dnsmasq hosts and addnhosts files, even if empty This fixes the problem reported in: https://bugzilla.redhat.com/show_bug.cgi?id=868389 Previously, the dnsmasq hosts file (used for static dhcp entries, and addnhosts file (used for additional dns host entries) were only created/referenced on the dnsmasq commandline if there was something to put in them at the time the network was started. Once we can update a network definition while it's active (which is now possible with virNetworkUpdate), this is no longer a valid strategy - if there were 0 dhcp static hosts (resulting in no reference to the hosts file on the commandline), then one was later added, the commandline wouldn't have linked dnsmasq up to the file, so even though we create it, dnsmasq doesn't pay any attention. The solution is to just always create these files and reference them on the dnsmasq commandline (almost always, anyway). That way dnsmasq can notice when a new entry is added at runtime (a SIGHUP is sent to dnsmasq by virNetworkUdpate whenever a host entry is added or removed) The exception to this is that the dhcp static hosts file isn't created if there are no lease ranges *and* no static hosts. This is because in this case dnsmasq won't be setup to listen for dhcp requests anyway - in that case, if the count of dhcp hosts goes from 0 to 1, dnsmasq will need to be restarted anyway (to get it listening on the dhcp port). Likewise, if the dhcp hosts count goes from 1 to 0 (and there are no dhcp ranges) we need to restart dnsmasq so that it will stop listening on port 67. These special situations are handled in the bridge driver's networkUpdate() by checking for ((bool) nranges||nhosts) both before and after the update, and triggering a dnsmasq restart if the before and after don't match.
2012-10-19 20:15:44 +00:00
/* even if there are 0 hosts, create a 0 length file, to allow
* for runtime addition.
*/
tmp = g_strdup_printf("%s.new", path);
if (!(f = fopen(tmp, "w"))) {
istmp = false;
if (!(f = fopen(path, "w")))
return -errno;
}
for (i = 0; i < nhosts; i++) {
if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) {
rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
return rc;
}
for (j = 0; j < hosts[i].nhostnames; j++) {
if (fputs(hosts[i].hostnames[j], f) == EOF || fputc('\t', f) == EOF) {
rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
return rc;
}
}
if (fputc('\n', f) == EOF) {
rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
return rc;
}
}
if (VIR_FCLOSE(f) == EOF)
return -errno;
if (istmp && rename(tmp, path) < 0) {
rc = -errno;
unlink(tmp);
return rc;
}
return 0;
}
static int
addnhostsSave(dnsmasqAddnHostsfile *addnhostsfile)
{
int err = addnhostsWrite(addnhostsfile->path, addnhostsfile->hosts,
addnhostsfile->nhosts);
if (err < 0) {
virReportSystemError(-err, _("cannot write config file '%s'"),
addnhostsfile->path);
return -1;
}
return 0;
}
static int
genericFileDelete(char *path)
{
if (!virFileExists(path))
return 0;
if (unlink(path) < 0) {
virReportSystemError(errno, _("cannot remove config file '%s'"),
path);
return -1;
}
return 0;
}
static void
hostsfileFree(dnsmasqHostsfile *hostsfile)
{
size_t i;
if (hostsfile->hosts) {
for (i = 0; i < hostsfile->nhosts; i++)
dhcphostFreeContent(&hostsfile->hosts[i]);
g_free(hostsfile->hosts);
hostsfile->nhosts = 0;
}
g_free(hostsfile->path);
g_free(hostsfile);
}
/* Note: There are many additional dhcp-host specifications
* supported by dnsmasq. There are only the basic ones.
*/
static int
hostsfileAdd(dnsmasqHostsfile *hostsfile,
const char *mac,
virSocketAddr *ip,
const char *name,
const char *id,
const char *leasetime,
bool ipv6)
{
g_autofree char *ipstr = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
VIR_REALLOC_N(hostsfile->hosts, hostsfile->nhosts + 1);
if (!(ipstr = virSocketAddrFormat(ip)))
return -1;
/* the first test determines if it is a dhcpv6 host */
if (ipv6) {
if (name && id) {
virBufferAsprintf(&buf, "id:%s,%s", id, name);
} else if (name && !id) {
virBufferAsprintf(&buf, "%s", name);
} else if (!name && id) {
virBufferAsprintf(&buf, "id:%s", id);
}
virBufferAsprintf(&buf, ",[%s]", ipstr);
} else if (name && mac) {
virBufferAsprintf(&buf, "%s,%s,%s", mac, ipstr, name);
} else if (name && !mac) {
virBufferAsprintf(&buf, "%s,%s", name, ipstr);
} else {
virBufferAsprintf(&buf, "%s,%s", mac, ipstr);
}
if (leasetime)
virBufferAsprintf(&buf, ",%s", leasetime);
if (!(hostsfile->hosts[hostsfile->nhosts].host = virBufferContentAndReset(&buf)))
return -1;
hostsfile->nhosts++;
return 0;
}
static dnsmasqHostsfile *
hostsfileNew(const char *name,
const char *config_dir)
{
dnsmasqHostsfile *hostsfile;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
hostsfile = g_new0(dnsmasqHostsfile, 1);
hostsfile->hosts = NULL;
hostsfile->nhosts = 0;
virBufferAsprintf(&buf, "%s", config_dir);
virBufferEscapeString(&buf, "/%s", name);
virBufferAsprintf(&buf, ".%s", DNSMASQ_HOSTSFILE_SUFFIX);
if (!(hostsfile->path = virBufferContentAndReset(&buf)))
goto error;
return hostsfile;
error:
hostsfileFree(hostsfile);
return NULL;
}
static int
hostsfileWrite(const char *path,
dnsmasqDhcpHost *hosts,
unsigned int nhosts)
{
g_autofree char *tmp = NULL;
FILE *f;
bool istmp = true;
size_t i;
int rc = 0;
network: always create dnsmasq hosts and addnhosts files, even if empty This fixes the problem reported in: https://bugzilla.redhat.com/show_bug.cgi?id=868389 Previously, the dnsmasq hosts file (used for static dhcp entries, and addnhosts file (used for additional dns host entries) were only created/referenced on the dnsmasq commandline if there was something to put in them at the time the network was started. Once we can update a network definition while it's active (which is now possible with virNetworkUpdate), this is no longer a valid strategy - if there were 0 dhcp static hosts (resulting in no reference to the hosts file on the commandline), then one was later added, the commandline wouldn't have linked dnsmasq up to the file, so even though we create it, dnsmasq doesn't pay any attention. The solution is to just always create these files and reference them on the dnsmasq commandline (almost always, anyway). That way dnsmasq can notice when a new entry is added at runtime (a SIGHUP is sent to dnsmasq by virNetworkUdpate whenever a host entry is added or removed) The exception to this is that the dhcp static hosts file isn't created if there are no lease ranges *and* no static hosts. This is because in this case dnsmasq won't be setup to listen for dhcp requests anyway - in that case, if the count of dhcp hosts goes from 0 to 1, dnsmasq will need to be restarted anyway (to get it listening on the dhcp port). Likewise, if the dhcp hosts count goes from 1 to 0 (and there are no dhcp ranges) we need to restart dnsmasq so that it will stop listening on port 67. These special situations are handled in the bridge driver's networkUpdate() by checking for ((bool) nranges||nhosts) both before and after the update, and triggering a dnsmasq restart if the before and after don't match.
2012-10-19 20:15:44 +00:00
/* even if there are 0 hosts, create a 0 length file, to allow
* for runtime addition.
*/
tmp = g_strdup_printf("%s.new", path);
if (!(f = fopen(tmp, "w"))) {
istmp = false;
if (!(f = fopen(path, "w")))
return -errno;
}
for (i = 0; i < nhosts; i++) {
if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
return rc;
}
}
if (VIR_FCLOSE(f) == EOF)
return -errno;
if (istmp && rename(tmp, path) < 0) {
rc = -errno;
unlink(tmp);
return rc;
}
return 0;
}
static int
hostsfileSave(dnsmasqHostsfile *hostsfile)
{
int err = hostsfileWrite(hostsfile->path, hostsfile->hosts,
hostsfile->nhosts);
if (err < 0) {
virReportSystemError(-err, _("cannot write config file '%s'"),
hostsfile->path);
return -1;
}
return 0;
}
/**
* dnsmasqContextNew:
*
* Create a new Dnsmasq context
*
* Returns a pointer to the new structure or NULL in case of error
*/
dnsmasqContext *
dnsmasqContextNew(const char *network_name,
const char *config_dir)
{
dnsmasqContext *ctx;
ctx = g_new0(dnsmasqContext, 1);
ctx->config_dir = g_strdup(config_dir);
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
goto error;
if (!(ctx->addnhostsfile = addnhostsNew(network_name, config_dir)))
goto error;
return ctx;
error:
dnsmasqContextFree(ctx);
return NULL;
}
/**
* dnsmasqContextFree:
* @ctx: pointer to the dnsmasq context
*
* Free the resources associated with a dnsmasq context
*/
void
dnsmasqContextFree(dnsmasqContext *ctx)
{
if (!ctx)
return;
g_free(ctx->config_dir);
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
if (ctx->hostsfile)
hostsfileFree(ctx->hostsfile);
if (ctx->addnhostsfile)
addnhostsFree(ctx->addnhostsfile);
g_free(ctx);
}
/**
* dnsmasqAddDhcpHost:
* @ctx: pointer to the dnsmasq context for each network
* @mac: pointer to the string contains mac address of the host
* @ip: pointer to the socket address contains ip of the host
* @name: pointer to the string contains hostname of the host or NULL
*
* Add dhcp-host entry.
*/
int
dnsmasqAddDhcpHost(dnsmasqContext *ctx,
const char *mac,
virSocketAddr *ip,
const char *name,
const char *id,
const char *leasetime,
bool ipv6)
{
return hostsfileAdd(ctx->hostsfile, mac, ip, name, id, leasetime, ipv6);
}
/*
* dnsmasqAddHost:
* @ctx: pointer to the dnsmasq context for each network
* @ip: pointer to the socket address contains ip of the host
* @name: pointer to the string contains hostname of the host
*
* Add additional host entry.
*/
int
dnsmasqAddHost(dnsmasqContext *ctx,
virSocketAddr *ip,
const char *name)
{
return addnhostsAdd(ctx->addnhostsfile, ip, name);
}
/**
* dnsmasqSave:
* @ctx: pointer to the dnsmasq context for each network
*
* Saves all the configurations associated with a context to disk.
*/
int
dnsmasqSave(const dnsmasqContext *ctx)
{
int ret = 0;
if (g_mkdir_with_parents(ctx->config_dir, 0777) < 0) {
virReportSystemError(errno, _("cannot create config directory '%s'"),
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
ctx->config_dir);
return -1;
}
if (ctx->hostsfile)
ret = hostsfileSave(ctx->hostsfile);
if (ret == 0) {
if (ctx->addnhostsfile)
ret = addnhostsSave(ctx->addnhostsfile);
}
return ret;
}
/**
* dnsmasqDelete:
* @ctx: pointer to the dnsmasq context for each network
*
* Delete all the configuration files associated with a context.
*/
int
dnsmasqDelete(const dnsmasqContext *ctx)
{
int ret = 0;
if (ctx->hostsfile)
ret = genericFileDelete(ctx->hostsfile->path);
if (ctx->addnhostsfile)
ret = genericFileDelete(ctx->addnhostsfile->path);
return ret;
}
/**
* dnsmasqReload:
* @pid: the pid of the target dnsmasq process
*
* Reloads all the configurations associated to a context
*/
int
dnsmasqReload(pid_t pid G_GNUC_UNUSED)
{
#ifndef WIN32
if (kill(pid, SIGHUP) != 0) {
virReportSystemError(errno,
_("Failed to make dnsmasq (PID: %d)"
" reload config files."),
pid);
return -1;
}
#endif /* WIN32 */
return 0;
}
/*
* dnsmasqCapabilities functions - provide useful information about the
* version of dnsmasq on this machine.
*
*/
struct _dnsmasqCaps {
virObject parent;
char *binaryPath;
unsigned long version;
};
static virClass *dnsmasqCapsClass;
static void
dnsmasqCapsDispose(void *obj)
{
dnsmasqCaps *caps = obj;
g_free(caps->binaryPath);
}
static int dnsmasqCapsOnceInit(void)
{
if (!VIR_CLASS_NEW(dnsmasqCaps, virClassForObject()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(dnsmasqCaps);
#define DNSMASQ_VERSION_STR "Dnsmasq version "
static int
dnsmasqCapsSetFromBuffer(dnsmasqCaps *caps, const char *buf)
{
int len;
const char *p;
p = STRSKIP(buf, DNSMASQ_VERSION_STR);
if (!p)
goto error;
virSkipToDigit(&p);
if (virParseVersionString(p, &caps->version, true) < 0)
goto error;
if (caps->version < DNSMASQ_MIN_MAJOR * 1000000 + DNSMASQ_MIN_MINOR * 1000) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("dnsmasq version >= %u.%u required but %lu.%lu found"),
DNSMASQ_MIN_MAJOR, DNSMASQ_MIN_MINOR,
caps->version / 1000000,
caps->version % 1000000 / 1000);
goto error;
}
VIR_INFO("dnsmasq version is %d.%d",
2012-12-17 17:49:18 +00:00
(int)caps->version / 1000000,
(int)(caps->version % 1000000) / 1000);
return 0;
error:
p = strchr(buf, '\n');
if (!p)
len = strlen(buf);
else
len = p - buf;
virReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot parse %s version number in '%.*s'"),
caps->binaryPath, len, buf);
return -1;
}
static int
dnsmasqCapsRefreshInternal(dnsmasqCaps *caps)
{
g_autoptr(virCommand) vercmd = NULL;
g_autoptr(virCommand) helpcmd = NULL;
g_autofree char *help = NULL;
g_autofree char *version = NULL;
g_autofree char *complete = NULL;
/* Make sure the binary we are about to try exec'ing exists.
* Technically we could catch the exec() failure, but that's
* in a sub-process so it's hard to feed back a useful error.
*/
if (!virFileIsExecutable(caps->binaryPath)) {
virReportSystemError(errno, _("dnsmasq binary %s is not executable"),
caps->binaryPath);
return -1;
}
vercmd = virCommandNewArgList(caps->binaryPath, "--version", NULL);
virCommandSetOutputBuffer(vercmd, &version);
virCommandAddEnvPassCommon(vercmd);
virCommandClearCaps(vercmd);
if (virCommandRun(vercmd, NULL) < 0)
return -1;
helpcmd = virCommandNewArgList(caps->binaryPath, "--help", NULL);
virCommandSetOutputBuffer(helpcmd, &help);
virCommandAddEnvPassCommon(helpcmd);
virCommandClearCaps(helpcmd);
if (virCommandRun(helpcmd, NULL) < 0)
return -1;
complete = g_strdup_printf("%s\n%s", version, help);
return dnsmasqCapsSetFromBuffer(caps, complete);
}
static dnsmasqCaps *
dnsmasqCapsNewEmpty(void)
{
g_autoptr(dnsmasqCaps) caps = NULL;
if (dnsmasqCapsInitialize() < 0)
return NULL;
if (!(caps = virObjectNew(dnsmasqCapsClass)))
return NULL;
caps->binaryPath = g_strdup(DNSMASQ);
return g_steal_pointer(&caps);
}
dnsmasqCaps *
dnsmasqCapsNewFromBuffer(const char *buf)
{
g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty();
if (!caps)
return NULL;
if (dnsmasqCapsSetFromBuffer(caps, buf) < 0)
return NULL;
return g_steal_pointer(&caps);
}
dnsmasqCaps *
dnsmasqCapsNewFromBinary(void)
{
g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty();
if (!caps)
return NULL;
if (dnsmasqCapsRefreshInternal(caps) < 0)
return NULL;
return g_steal_pointer(&caps);
}
const char *
dnsmasqCapsGetBinaryPath(dnsmasqCaps *caps)
{
return caps ? caps->binaryPath : DNSMASQ;
}
/** dnsmasqDhcpHostsToString:
*
* Turns a vector of dnsmasqDhcpHost into the string that is ought to be
* stored in the hostsfile, this functionality is split to make hostsfiles
* testable. Returns NULL if nhosts is 0.
*/
char *
dnsmasqDhcpHostsToString(dnsmasqDhcpHost *hosts,
unsigned int nhosts)
{
size_t i;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
for (i = 0; i < nhosts; i++)
virBufferAsprintf(&buf, "%s\n", hosts[i].host);
return virBufferContentAndReset(&buf);
}