libvirt/src/xen/xen_inotify.c

444 lines
13 KiB
C
Raw Normal View History

/*
* xen_inofify.c: Xen notification of xml file activity in the
* following dirs:
* /etc/xen
* /var/lib/xend/domains
*
* Copyright (C) 2010-2013 Red Hat, Inc.
* Copyright (C) 2008 VirtualIron
*
* 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/>.
*
* Author: Ben Guthro
*/
#include <config.h>
#include <dirent.h>
#include <sys/inotify.h>
#include "virerror.h"
#include "datatypes.h"
#include "driver.h"
2012-12-12 18:06:53 +00:00
#include "viralloc.h"
#include "xen_driver.h"
2012-12-12 16:35:35 +00:00
#include "virconf.h"
#include "domain_conf.h"
#include "xen_inotify.h"
#include "xend_internal.h"
2012-12-12 17:59:27 +00:00
#include "virlog.h"
2012-12-13 18:01:25 +00:00
#include "viruuid.h"
#include "virfile.h"
#include "virstring.h"
#include "xm_internal.h" /* for xenXMDomainConfigParse */
#define VIR_FROM_THIS VIR_FROM_XEN_INOTIFY
2008-12-04 21:09:20 +00:00
static int
xenInotifyXenCacheLookup(virConnectPtr conn,
const char *filename,
char **name,
unsigned char *uuid)
{
xenUnifiedPrivatePtr priv = conn->privateData;
xenXMConfCachePtr entry;
if (!(entry = virHashLookup(priv->configCache, filename))) {
VIR_DEBUG("No config found for %s", filename);
2008-12-04 21:09:20 +00:00
return -1;
}
2008-12-04 21:09:20 +00:00
memcpy(uuid, entry->def->uuid, VIR_UUID_BUFLEN);
if (VIR_STRDUP(*name, entry->def->name) < 0) {
VIR_DEBUG("Error getting dom from def");
2008-12-04 21:09:20 +00:00
return -1;
}
2008-12-04 21:09:20 +00:00
return 0;
}
2008-12-04 21:09:20 +00:00
static int
xenInotifyXendDomainsDirLookup(virConnectPtr conn,
const char *filename,
char **name,
unsigned char *uuid)
{
size_t i;
virDomainDefPtr def;
const char *uuid_str;
2008-12-04 21:09:20 +00:00
unsigned char rawuuid[VIR_UUID_BUFLEN];
xenUnifiedPrivatePtr priv = conn->privateData;
/* xend is managing domains. we will get
* a filename in the manner:
* /var/lib/xend/domains/<uuid>/
*/
uuid_str = filename + strlen(XEND_DOMAINS_DIR) + 1;
2008-12-04 21:09:20 +00:00
if (virUUIDParse(uuid_str, rawuuid) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("parsing uuid %s"), uuid_str);
2008-12-04 21:09:20 +00:00
return -1;
}
/* call directly into xend here, as driver may not yet
be set during open while we are building our
initial list of domains */
VIR_DEBUG("Looking for dom with uuid: %s", uuid_str);
if (!(def = xenDaemonLookupByUUID(conn, rawuuid))) {
/* If we are here, the domain has gone away.
search for, and create a domain from the stored
list info */
for (i = 0; i < priv->configInfoList->count; i++) {
if (!memcmp(rawuuid, priv->configInfoList->doms[i]->uuid, VIR_UUID_BUFLEN)) {
if (VIR_STRDUP(*name, priv->configInfoList->doms[i]->name) < 0)
2008-12-04 21:09:20 +00:00
return -1;
memcpy(uuid, priv->configInfoList->doms[i]->uuid, VIR_UUID_BUFLEN);
VIR_DEBUG("Found dom on list");
2008-12-04 21:09:20 +00:00
return 0;
}
}
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("finding dom on config list"));
2008-12-04 21:09:20 +00:00
return -1;
}
if (VIR_STRDUP(*name, def->name) < 0) {
virDomainDefFree(def);
2008-12-04 21:09:20 +00:00
return -1;
}
memcpy(uuid, def->uuid, VIR_UUID_BUFLEN);
virDomainDefFree(def);
/* succeeded too find domain by uuid */
2008-12-04 21:09:20 +00:00
return 0;
}
2008-12-04 21:09:20 +00:00
static int
xenInotifyDomainLookup(virConnectPtr conn,
const char *filename,
char **name,
unsigned char *uuid)
{
xenUnifiedPrivatePtr priv = conn->privateData;
if (priv->useXenConfigCache)
return xenInotifyXenCacheLookup(conn, filename, name, uuid);
2008-12-04 21:09:20 +00:00
else
return xenInotifyXendDomainsDirLookup(conn, filename, name, uuid);
}
static virObjectEventPtr
2008-12-04 21:09:20 +00:00
xenInotifyDomainEventFromFile(virConnectPtr conn,
const char *filename,
int type,
int detail)
{
virObjectEventPtr event;
2008-12-04 21:09:20 +00:00
char *name = NULL;
unsigned char uuid[VIR_UUID_BUFLEN];
if (xenInotifyDomainLookup(conn, filename, &name, uuid) < 0)
return NULL;
event = virDomainEventLifecycleNew(-1, name, uuid, type, detail);
2008-12-04 21:09:20 +00:00
VIR_FREE(name);
return event;
}
static int
xenInotifyXendDomainsDirRemoveEntry(virConnectPtr conn, const char *fname)
{
xenUnifiedPrivatePtr priv = conn->privateData;
const char *uuidstr = fname + strlen(XEND_DOMAINS_DIR) + 1;
unsigned char uuid[VIR_UUID_BUFLEN];
size_t i;
if (virUUIDParse(uuidstr, uuid) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("parsing uuid %s"), uuidstr);
return -1;
}
/* match and remove on uuid */
for (i = 0; i < priv->configInfoList->count; i++) {
if (!memcmp(uuid, priv->configInfoList->doms[i]->uuid, VIR_UUID_BUFLEN)) {
VIR_FREE(priv->configInfoList->doms[i]->name);
VIR_FREE(priv->configInfoList->doms[i]);
VIR_DELETE_ELEMENT(priv->configInfoList->doms, i,
priv->configInfoList->count);
return 0;
}
}
return -1;
}
static int
xenInotifyXendDomainsDirAddEntry(virConnectPtr conn, const char *fname)
{
2008-12-04 21:09:20 +00:00
char *name = NULL;
unsigned char uuid[VIR_UUID_BUFLEN];
xenUnifiedPrivatePtr priv = conn->privateData;
2008-12-04 21:09:20 +00:00
if (xenInotifyDomainLookup(conn, fname, &name, uuid) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error looking up domain"));
return -1;
}
if (xenUnifiedAddDomainInfo(priv->configInfoList,
2008-12-04 21:09:20 +00:00
-1, name, uuid) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config cache"));
2008-12-04 21:09:20 +00:00
VIR_FREE(name);
return -1;
}
2008-12-04 21:09:20 +00:00
VIR_FREE(name);
return 0;
}
static int
xenInotifyRemoveDomainConfigInfo(virConnectPtr conn, const char *fname)
{
xenUnifiedPrivatePtr priv = conn->privateData;
return priv->useXenConfigCache ?
xenXMConfigCacheRemoveFile(conn, fname) :
xenInotifyXendDomainsDirRemoveEntry(conn, fname);
}
static int
xenInotifyAddDomainConfigInfo(virConnectPtr conn, const char *fname)
{
xenUnifiedPrivatePtr priv = conn->privateData;
return priv->useXenConfigCache ?
xenXMConfigCacheAddFile(conn, fname) :
xenInotifyXendDomainsDirAddEntry(conn, fname);
}
static void
xenInotifyEvent(int watch ATTRIBUTE_UNUSED,
int fd,
int events ATTRIBUTE_UNUSED,
void *data)
{
char buf[1024];
char fname[1024];
struct inotify_event *e;
int got;
char *tmp, *name;
virConnectPtr conn = data;
xenUnifiedPrivatePtr priv = NULL;
VIR_DEBUG("got inotify event");
if (conn && conn->privateData) {
priv = conn->privateData;
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("conn, or private data is NULL"));
return;
}
2009-01-21 18:11:14 +00:00
xenUnifiedLock(priv);
reread:
got = read(fd, buf, sizeof(buf));
if (got == -1) {
if (errno == EINTR)
goto reread;
2009-01-21 18:11:14 +00:00
goto cleanup;
}
tmp = buf;
while (got) {
if (got < sizeof(struct inotify_event))
2009-01-21 18:11:14 +00:00
goto cleanup; /* bad */
VIR_WARNINGS_NO_CAST_ALIGN
e = (struct inotify_event *)tmp;
VIR_WARNINGS_RESET
tmp += sizeof(struct inotify_event);
got -= sizeof(struct inotify_event);
if (got < e->len)
2009-01-21 18:11:14 +00:00
goto cleanup;
tmp += e->len;
got -= e->len;
name = (char *)&(e->name);
snprintf(fname, 1024, "%s/%s",
priv->configDir, name);
if (e->mask & (IN_DELETE | IN_MOVED_FROM)) {
virObjectEventPtr event =
2008-12-04 21:09:20 +00:00
xenInotifyDomainEventFromFile(conn, fname,
VIR_DOMAIN_EVENT_UNDEFINED,
VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
if (event)
2008-12-04 21:09:20 +00:00
xenUnifiedDomainEventDispatch(conn->privateData, event);
else
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("looking up dom"));
if (xenInotifyRemoveDomainConfigInfo(conn, fname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config cache"));
2009-01-21 18:11:14 +00:00
goto cleanup;
}
} else if (e->mask & (IN_CREATE | IN_CLOSE_WRITE | IN_MOVED_TO)) {
virObjectEventPtr event;
if (xenInotifyAddDomainConfigInfo(conn, fname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config cache"));
2009-01-21 18:11:14 +00:00
goto cleanup;
}
2008-12-04 21:09:20 +00:00
event = xenInotifyDomainEventFromFile(conn, fname,
VIR_DOMAIN_EVENT_DEFINED,
VIR_DOMAIN_EVENT_DEFINED_ADDED);
if (event)
xenUnifiedDomainEventDispatch(conn->privateData, event);
else
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("looking up dom"));
}
}
2009-01-21 18:11:14 +00:00
cleanup:
xenUnifiedUnlock(priv);
}
/**
* xenInotifyOpen:
* @conn: pointer to the connection block
* @name: URL for the target, NULL for local
* @flags: combination of virDrvOpenFlag(s)
*
* Connects and starts listening for inotify events
*
* Returns 0 or -1 in case of error.
*/
int
xenInotifyOpen(virConnectPtr conn,
drivers: prefer unsigned int for flags Now that the public APIs always use unsigned flags, the internal driver callbacks might as well do likewise. * src/driver.h (vrDrvOpen, virDrvDomainCoreDump) (virDrvDomainGetXMLDesc, virDrvNetworkGetXMLDesc) (virDrvNWFilterGetXMLDesc): Update type. * src/remote/remote_protocol.x (remote_open_args) (remote_domain_core_dump_args, remote_domain_get_xml_desc_args) (remote_network_get_xml_desc_args) (remote_nwfilter_get_xml_desc_args): Likewise. * src/test/test_driver.c: Update clients. * src/remote/remote_driver.c: Likewise. * src/xen/xen_hypervisor.c: Likewise. * src/xen/xen_hypervisor.h: Likewise. * src/xen/xen_driver.c: Likewise. * src/xen/xend_internal.c: Likewise. * src/xen/xend_internal.h: Likewise. * src/xen/xm_internal.c: Likewise. * src/xen/xm_internal.h: Likewise. * src/xen/xs_internal.c: Likewise. * src/xen/xs_internal.h: Likewise. * src/xen/xen_inotify.c: Likewise. * src/xen/xen_inotify.h: Likewise. * src/phyp/phyp_driver.c: Likewise. * src/openvz/openvz_driver.c: Likewise. * src/vmware/vmware_driver.c: Likewise. * src/vbox/vbox_driver.c: Likewise. * src/vbox/vbox_tmpl.c: Likewise. * src/xenapi/xenapi_driver.c: Likewise. * src/esx/esx_driver.c: Likewise. * src/esx/esx_interface_driver.c: Likewise. * src/esx/esx_network_driver.c: Likewise. * src/esx/esx_storage_driver.c: Likewise. * src/esx/esx_device_monitor.c: Likewise. * src/esx/esx_secret_driver.c: Likewise. * src/esx/esx_nwfilter_driver.c: Likewise. * src/interface/netcf_driver.c: Likewise. * src/nwfilter/nwfilter_driver.c: Likewise. * src/libxl/libxl_driver.c: Likewise. * src/qemu/qemu_driver.c: Likewise. * src/lxc/lxc_driver.c: Likewise. * src/uml/uml_driver.c: Likewise. * src/network/bridge_driver.c: Likewise. * src/secret/secret_driver.c: Likewise. * src/storage/storage_driver.c: Likewise. * src/node_device/node_device_hal.c: Likewise. * src/node_device/node_device_udev.c: Likewise. * src/remote_protocol-structs: Likewise.
2011-07-06 20:40:19 +00:00
virConnectAuthPtr auth ATTRIBUTE_UNUSED,
unsigned int flags)
{
DIR *dh;
struct dirent *ent;
char *path;
xenUnifiedPrivatePtr priv = conn->privateData;
virCheckFlags(VIR_CONNECT_RO, -1);
if (priv->configDir) {
priv->useXenConfigCache = 1;
} else {
/* /var/lib/xend/domains/<uuid>/config.sxp */
priv->configDir = XEND_DOMAINS_DIR;
priv->useXenConfigCache = 0;
if (VIR_ALLOC(priv->configInfoList) < 0)
return -1;
/* populate initial list */
if (!(dh = opendir(priv->configDir))) {
virReportSystemError(errno,
_("cannot open directory: %s"),
priv->configDir);
return -1;
}
while ((ent = readdir(dh))) {
if (STRPREFIX(ent->d_name, "."))
continue;
/* Build the full file path */
if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
closedir(dh);
return -1;
}
if (xenInotifyAddDomainConfigInfo(conn, path) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config list"));
closedir(dh);
VIR_FREE(path);
return -1;
}
VIR_FREE(path);
}
2008-11-30 18:37:35 +00:00
closedir(dh);
}
if ((priv->inotifyFD = inotify_init()) < 0) {
virReportSystemError(errno,
"%s", _("initializing inotify"));
return -1;
}
VIR_DEBUG("Adding a watch on %s", priv->configDir);
if (inotify_add_watch(priv->inotifyFD,
priv->configDir,
IN_CREATE |
IN_CLOSE_WRITE | IN_DELETE |
IN_MOVED_TO | IN_MOVED_FROM) < 0) {
virReportSystemError(errno,
_("adding watch on %s"),
priv->configDir);
return -1;
}
VIR_DEBUG("Building initial config cache");
if (priv->useXenConfigCache &&
xenXMConfigCacheRefresh(conn) < 0) {
VIR_DEBUG("Failed to enable XM config cache %s", conn->err.message);
return -1;
}
VIR_DEBUG("Registering with event loop");
/* Add the handle for monitoring */
if ((priv->inotifyWatch = virEventAddHandle(priv->inotifyFD, VIR_EVENT_HANDLE_READABLE,
xenInotifyEvent, conn, NULL)) < 0) {
VIR_DEBUG("Failed to add inotify handle, disabling events");
}
return 0;
}
/**
* xenInotifyClose:
* @conn: pointer to the connection block
*
* Close and stop listening for inotify events
*
* Returns 0 in case of success or -1 in case of error.
*/
int
xenInotifyClose(virConnectPtr conn)
{
xenUnifiedPrivatePtr priv = conn->privateData;
if (priv->configInfoList)
xenUnifiedDomainInfoListFree(priv->configInfoList);
if (priv->inotifyWatch != -1)
virEventRemoveHandle(priv->inotifyWatch);
VIR_FORCE_CLOSE(priv->inotifyFD);
return 0;
}