events: Add explicit lookup 'key' value

This allows event implementations to match on something other
than an object's uuid, like nodedev or interface objects which
don't have a uuid.
This commit is contained in:
Cole Robinson 2016-06-23 12:06:39 -04:00
parent d7c96a7245
commit 37d1c246e5
5 changed files with 33 additions and 19 deletions

View File

@ -581,6 +581,7 @@ virDomainEventNew(virClassPtr klass,
const unsigned char *uuid) const unsigned char *uuid)
{ {
virDomainEventPtr event; virDomainEventPtr event;
char uuidstr[VIR_UUID_STRING_BUFLEN];
if (virDomainEventsInitialize() < 0) if (virDomainEventsInitialize() < 0)
return NULL; return NULL;
@ -592,10 +593,14 @@ virDomainEventNew(virClassPtr klass,
return NULL; return NULL;
} }
/* We use uuid for matching key. We ignore 'name' because
* Xen sometimes renames guests during migration, thus
* 'uuid' is the only truly reliable key we can use. */
virUUIDFormat(uuid, uuidstr);
if (!(event = virObjectEventNew(klass, if (!(event = virObjectEventNew(klass,
virDomainEventDispatchDefaultFunc, virDomainEventDispatchDefaultFunc,
eventID, eventID,
id, name, uuid))) id, name, uuid, uuidstr)))
return NULL; return NULL;
return (virObjectEventPtr)event; return (virObjectEventPtr)event;
@ -1873,13 +1878,15 @@ virDomainQemuMonitorEventNew(int id,
const char *details) const char *details)
{ {
virDomainQemuMonitorEventPtr ev; virDomainQemuMonitorEventPtr ev;
char uuidstr[VIR_UUID_STRING_BUFLEN];
if (virDomainEventsInitialize() < 0) if (virDomainEventsInitialize() < 0)
return NULL; return NULL;
virUUIDFormat(uuid, uuidstr);
if (!(ev = virObjectEventNew(virDomainQemuMonitorEventClass, if (!(ev = virObjectEventNew(virDomainQemuMonitorEventClass,
virDomainQemuMonitorEventDispatchFunc, virDomainQemuMonitorEventDispatchFunc,
0, id, name, uuid))) 0, id, name, uuid, uuidstr)))
return NULL; return NULL;
/* event is mandatory, details are optional */ /* event is mandatory, details are optional */

View File

@ -226,14 +226,16 @@ virNetworkEventLifecycleNew(const char *name,
int detail) int detail)
{ {
virNetworkEventLifecyclePtr event; virNetworkEventLifecyclePtr event;
char uuidstr[VIR_UUID_STRING_BUFLEN];
if (virNetworkEventsInitialize() < 0) if (virNetworkEventsInitialize() < 0)
return NULL; return NULL;
virUUIDFormat(uuid, uuidstr);
if (!(event = virObjectEventNew(virNetworkEventLifecycleClass, if (!(event = virObjectEventNew(virNetworkEventLifecycleClass,
virNetworkEventDispatchDefaultFunc, virNetworkEventDispatchDefaultFunc,
VIR_NETWORK_EVENT_ID_LIFECYCLE, VIR_NETWORK_EVENT_ID_LIFECYCLE,
0, name, uuid))) 0, name, uuid, uuidstr)))
return NULL; return NULL;
event->type = type; event->type = type;

View File

@ -123,6 +123,7 @@ virObjectEventDispose(void *obj)
VIR_DEBUG("obj=%p", event); VIR_DEBUG("obj=%p", event);
VIR_FREE(event->meta.name); VIR_FREE(event->meta.name);
VIR_FREE(event->meta.key);
} }
/** /**
@ -619,6 +620,7 @@ virObjectEventStateNew(void)
* @id: id of the object the event describes, or 0 * @id: id of the object the event describes, or 0
* @name: name of the object the event describes * @name: name of the object the event describes
* @uuid: uuid of the object the event describes * @uuid: uuid of the object the event describes
* @key: key for per-object filtering
* *
* Create a new event, with the information common to all events. * Create a new event, with the information common to all events.
*/ */
@ -628,7 +630,8 @@ virObjectEventNew(virClassPtr klass,
int eventID, int eventID,
int id, int id,
const char *name, const char *name,
const unsigned char *uuid) const unsigned char *uuid,
const char *key)
{ {
virObjectEventPtr event; virObjectEventPtr event;
@ -653,6 +656,11 @@ virObjectEventNew(virClassPtr klass,
VIR_FREE(event); VIR_FREE(event);
return NULL; return NULL;
} }
if (VIR_STRDUP(event->meta.key, key) < 0) {
VIR_FREE(event->meta.name);
VIR_FREE(event);
return NULL;
}
event->meta.id = id; event->meta.id = id;
memcpy(event->meta.uuid, uuid, VIR_UUID_BUFLEN); memcpy(event->meta.uuid, uuid, VIR_UUID_BUFLEN);
@ -701,17 +709,8 @@ virObjectEventDispatchMatchCallback(virObjectEventPtr event,
if (cb->filter && !(cb->filter)(cb->conn, event, cb->filter_opaque)) if (cb->filter && !(cb->filter)(cb->conn, event, cb->filter_opaque))
return false; return false;
if (cb->uuid_filter) { if (cb->uuid_filter)
/* Deliberately ignoring 'id' for matching, since that return STREQ(event->meta.key, cb->uuid);
* will cause problems when a domain switches between
* running & shutoff states & ignoring 'name' since
* Xen sometimes renames guests during migration, thus
* leaving 'uuid' as the only truly reliable ID we can use. */
char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(event->meta.uuid, uuidstr);
return STREQ(uuidstr, cb->uuid);
}
return true; return true;
} }

View File

@ -31,6 +31,7 @@ struct _virObjectMeta {
int id; int id;
char *name; char *name;
unsigned char uuid[VIR_UUID_BUFLEN]; unsigned char uuid[VIR_UUID_BUFLEN];
char *key;
}; };
typedef struct _virObjectMeta virObjectMeta; typedef struct _virObjectMeta virObjectMeta;
typedef virObjectMeta *virObjectMetaPtr; typedef virObjectMeta *virObjectMetaPtr;
@ -102,8 +103,9 @@ virObjectEventNew(virClassPtr klass,
int eventID, int eventID,
int id, int id,
const char *name, const char *name,
const unsigned char *uuid) const unsigned char *uuid,
const char *key)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5)
ATTRIBUTE_NONNULL(6); ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7);
#endif #endif

View File

@ -259,14 +259,16 @@ virStoragePoolEventLifecycleNew(const char *name,
int detail) int detail)
{ {
virStoragePoolEventLifecyclePtr event; virStoragePoolEventLifecyclePtr event;
char uuidstr[VIR_UUID_STRING_BUFLEN];
if (virStoragePoolEventsInitialize() < 0) if (virStoragePoolEventsInitialize() < 0)
return NULL; return NULL;
virUUIDFormat(uuid, uuidstr);
if (!(event = virObjectEventNew(virStoragePoolEventLifecycleClass, if (!(event = virObjectEventNew(virStoragePoolEventLifecycleClass,
virStoragePoolEventDispatchDefaultFunc, virStoragePoolEventDispatchDefaultFunc,
VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE, VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE,
0, name, uuid))) 0, name, uuid, uuidstr)))
return NULL; return NULL;
event->type = type; event->type = type;
@ -288,14 +290,16 @@ virStoragePoolEventRefreshNew(const char *name,
const unsigned char *uuid) const unsigned char *uuid)
{ {
virStoragePoolEventRefreshPtr event; virStoragePoolEventRefreshPtr event;
char uuidstr[VIR_UUID_STRING_BUFLEN];
if (virStoragePoolEventsInitialize() < 0) if (virStoragePoolEventsInitialize() < 0)
return NULL; return NULL;
virUUIDFormat(uuid, uuidstr);
if (!(event = virObjectEventNew(virStoragePoolEventRefreshClass, if (!(event = virObjectEventNew(virStoragePoolEventRefreshClass,
virStoragePoolEventDispatchDefaultFunc, virStoragePoolEventDispatchDefaultFunc,
VIR_STORAGE_POOL_EVENT_ID_REFRESH, VIR_STORAGE_POOL_EVENT_ID_REFRESH,
0, name, uuid))) 0, name, uuid, uuidstr)))
return NULL; return NULL;
return (virObjectEventPtr)event; return (virObjectEventPtr)event;