but kept virDomainEventStateRegisterID as a convenience wrapper around this new function. --- src/conf/domain_event.c | 125 +++++++++++++++++++++++++++++++---------------- src/conf/domain_event.h | 25 ++++++++++ src/libvirt_private.syms | 1 + 3 files changed, 110 insertions(+), 41 deletions(-) diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index 00e20c2..fa5c190 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -32,6 +32,8 @@ #define VIR_FROM_THIS VIR_FROM_NONE +#define VIR_OBJECT_EVENT_CALLBACK(cb) ((virConnectObjectEventGenericCallback)(cb)) + struct _virObjectMeta { int id; char *name; @@ -68,7 +70,7 @@ struct _virObjectEventCallback { int eventID; virConnectPtr conn; virObjectMetaPtr meta; - virConnectDomainEventGenericCallback cb; + virConnectObjectEventGenericCallback cb; void *opaque; virFreeCallback freecb; int deleted; @@ -168,7 +170,7 @@ virDomainEventCallbackListRemove(virConnectPtr conn, int ret = 0; size_t i; for (i = 0; i < cbList->count; i++) { - if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && + if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE && cbList->callbacks[i]->conn == conn) { virFreeCallback freecb = cbList->callbacks[i]->freecb; @@ -261,7 +263,7 @@ virDomainEventCallbackListMarkDelete(virConnectPtr conn, int ret = 0; size_t i; for (i = 0; i < cbList->count; i++) { - if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && + if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE && cbList->callbacks[i]->conn == conn) { cbList->callbacks[i]->deleted = 1; @@ -355,7 +357,7 @@ virObjectEventCallbackListAddID(virConnectPtr conn, const char *name, int id, int eventID, - virConnectDomainEventGenericCallback callback, + virConnectObjectEventGenericCallback callback, void *opaque, virFreeCallback freecb, int *callbackID) @@ -371,7 +373,7 @@ virObjectEventCallbackListAddID(virConnectPtr conn, /* check if we already have this callback on our list */ for (i = 0; i < cbList->count; i++) { - if (cbList->callbacks[i]->cb == VIR_DOMAIN_EVENT_CALLBACK(callback) && + if (cbList->callbacks[i]->cb == VIR_OBJECT_EVENT_CALLBACK(callback) && cbList->callbacks[i]->eventID == eventID && cbList->callbacks[i]->conn == conn && ((uuid && cbList->callbacks[i]->meta && @@ -453,7 +455,7 @@ virDomainEventCallbackListAdd(virConnectPtr conn, { return virObjectEventCallbackListAddID(conn, cbList, NULL, NULL, 0, VIR_DOMAIN_EVENT_ID_LIFECYCLE, - VIR_DOMAIN_EVENT_CALLBACK(callback), + VIR_OBJECT_EVENT_CALLBACK(callback), opaque, freecb, NULL); } @@ -1235,9 +1237,9 @@ virObjectEventQueuePush(virObjectEventQueuePtr evtQueue, } -typedef void (*virDomainEventDispatchFunc)(virConnectPtr conn, +typedef void (*virObjectEventDispatchFunc)(virConnectPtr conn, virDomainEventPtr event, - virConnectDomainEventGenericCallback cb, + virConnectObjectEventGenericCallback cb, void *cbopaque, void *opaque); @@ -1400,7 +1402,7 @@ static int virDomainEventDispatchMatchCallback(virDomainEventPtr event, static void virDomainEventDispatch(virDomainEventPtr event, virObjectEventCallbackListPtr callbacks, - virDomainEventDispatchFunc dispatch, + virObjectEventDispatchFunc dispatch, void *opaque) { size_t i; @@ -1425,7 +1427,7 @@ virDomainEventDispatch(virDomainEventPtr event, static void virObjectEventQueueDispatch(virObjectEventQueuePtr queue, virObjectEventCallbackListPtr callbacks, - virDomainEventDispatchFunc dispatch, + virObjectEventDispatchFunc dispatch, void *opaque) { size_t i; @@ -1463,7 +1465,7 @@ virObjectEventStateQueue(virObjectEventStatePtr state, static void virObjectEventStateDispatchFunc(virConnectPtr conn, virDomainEventPtr event, - virConnectDomainEventGenericCallback cb, + virConnectObjectEventGenericCallback cb, void *cbopaque, void *opaque) { @@ -1471,7 +1473,8 @@ virObjectEventStateDispatchFunc(virConnectPtr conn, /* Drop the lock whle dispatching, for sake of re-entrancy */ virObjectEventStateUnlock(state); - virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL); + virDomainEventDispatchDefaultFunc(conn, event, + VIR_DOMAIN_EVENT_CALLBACK(cb), cbopaque, NULL); virObjectEventStateLock(state); } @@ -1506,6 +1509,65 @@ virObjectEventStateFlush(virObjectEventStatePtr state) /** + * virObjectEventStateRegisterID: + * @conn: connection to associate with callback + * @state: domain event state + * @eventID: ID of the event type to register for + * @cb: function to remove from event + * @opaque: data blob to pass to callback + * @freecb: callback to free @opaque + * @callbackID: filled with callback ID + * + * Register the function @callbackID with connection @conn, + * from @state, for events of type @eventID. + * + * Returns: the number of callbacks now registered, or -1 on error + */ +int +virObjectEventStateRegisterID(virConnectPtr conn, + virObjectEventStatePtr state, + unsigned char uuid[VIR_UUID_BUFLEN], + const char *name, + int id, + int eventID, + virConnectObjectEventGenericCallback cb, + void *opaque, + virFreeCallback freecb, + int *callbackID) +{ + int ret = -1; + + virObjectEventStateLock(state); + + if ((state->callbacks->count == 0) && + (state->timer == -1) && + (state->timer = virEventAddTimeout(-1, + virDomainEventTimer, + state, + NULL)) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("could not initialize domain event timer")); + goto cleanup; + } + + ret = virObjectEventCallbackListAddID(conn, state->callbacks, + uuid, name, id, eventID, cb, opaque, freecb, + callbackID); + + if (ret == -1 && + state->callbacks->count == 0 && + state->timer != -1) { + virEventRemoveTimeout(state->timer); + state->timer = -1; + } + +cleanup: + virObjectEventStateUnlock(state); + return ret; +} + + +/** * virDomainEventStateRegister: * @conn: connection to associate with callback * @state: object event state @@ -1581,35 +1643,16 @@ virDomainEventStateRegisterID(virConnectPtr conn, virFreeCallback freecb, int *callbackID) { - int ret = -1; - - virObjectEventStateLock(state); - - if ((state->callbacks->count == 0) && - (state->timer == -1) && - (state->timer = virEventAddTimeout(-1, - virDomainEventTimer, - state, - NULL)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("could not initialize domain event timer")); - goto cleanup; - } - - ret = virObjectEventCallbackListAddID(conn, state->callbacks, dom->uuid, - dom->name, dom->id, eventID, cb, - opaque, freecb, callbackID); - - if (ret == -1 && - state->callbacks->count == 0 && - state->timer != -1) { - virEventRemoveTimeout(state->timer); - state->timer = -1; - } - -cleanup: - virObjectEventStateUnlock(state); - return ret; + if (dom) + return virObjectEventStateRegisterID(conn, state, dom->uuid, dom->name, + dom->id, eventID, + VIR_OBJECT_EVENT_CALLBACK(cb), + opaque, freecb, callbackID); + else + return virObjectEventStateRegisterID(conn, state, NULL, NULL, 0, + eventID, + VIR_OBJECT_EVENT_CALLBACK(cb), + opaque, freecb, callbackID); } diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h index 65a17ad..937b113 100644 --- a/src/conf/domain_event.h +++ b/src/conf/domain_event.h @@ -143,6 +143,19 @@ void virObjectEventStateFree(virObjectEventStatePtr state); virObjectEventStatePtr virObjectEventStateNew(void); +/* + * virConnectObjectEventGenericCallback: + * @conn: the connection pointer + * @obj: the object pointer + * @opaque: application specified data + * + * A generic object event callback handler. Specific events usually + * have a customization with extra parameters + */ +typedef void (*virConnectObjectEventGenericCallback)(virConnectPtr conn, + void *obj, + void *opaque); + void virObjectEventStateQueue(virObjectEventStatePtr state, virDomainEventPtr event) @@ -168,6 +181,18 @@ virDomainEventStateDeregister(virConnectPtr conn, virConnectDomainEventCallback callback) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); int +virObjectEventStateRegisterID(virConnectPtr conn, + virObjectEventStatePtr state, + unsigned char uuid[VIR_UUID_BUFLEN], + const char *name, + int id, + int eventID, + virConnectObjectEventGenericCallback cb, + void *opaque, + virFreeCallback freecb, + int *callbackID) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(7); +int virObjectEventStateDeregisterID(virConnectPtr conn, virObjectEventStatePtr state, int callbackID) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3933b40..a14e290 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -455,6 +455,7 @@ virObjectEventStateEventID; virObjectEventStateFree; virObjectEventStateNew; virObjectEventStateQueue; +virObjectEventStateRegisterID; # conf/domain_nwfilter.h -- 1.8.4.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list