From ea7dc3378655baaa3f6d5b32cee89a5ad5d81262 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 18 Mar 2010 13:17:14 +0000 Subject: [PATCH] Make internal domain events struct definitions private The virtual box driver was directly accesing the domain events structs instead of using the APIs provided. To prevent this kind of abuse, make the struct definitions private, forcing use of the internal APIs. This requires adding one extra internal API. * src/conf/domain_event.h, src/conf/domain_event.c: Move virDomainEventCallback and virDomainEvent structs into the source file instead of header * src/vbox/vbox_tmpl.c: Use official APIs for dispatching domain events instead of accessing structs directly. --- src/conf/domain_event.c | 22 ++++++++++++++++++ src/conf/domain_event.h | 16 ++----------- src/vbox/vbox_tmpl.c | 51 ++++++++++++++++------------------------- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index e791cabe61..b520232a40 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -35,6 +35,21 @@ virReportErrorHelper(conn, VIR_FROM_THIS, code, __FILE__, \ __FUNCTION__, __LINE__, __VA_ARGS__) +struct _virDomainEventCallback { + virConnectPtr conn; + virConnectDomainEventCallback cb; + void *opaque; + virFreeCallback freecb; + int deleted; +}; + +struct _virDomainEvent { + int id; + char *name; + unsigned char uuid[VIR_UUID_BUFLEN]; + int type; + int detail; +}; /** * virDomainEventCallbackListFree: @@ -243,6 +258,13 @@ virDomainEventCallbackListAdd(virConnectPtr conn, return 0; } + +int virDomainEventCallbackListCount(virDomainEventCallbackListPtr cbList) +{ + return cbList->count; +} + + void virDomainEventFree(virDomainEventPtr event) { if (!event) diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h index 8d1b330b38..5d8faee59d 100644 --- a/src/conf/domain_event.h +++ b/src/conf/domain_event.h @@ -27,13 +27,6 @@ # include "domain_conf.h" -struct _virDomainEventCallback { - virConnectPtr conn; - virConnectDomainEventCallback cb; - void *opaque; - virFreeCallback freecb; - int deleted; -}; typedef struct _virDomainEventCallback virDomainEventCallback; typedef virDomainEventCallback *virDomainEventCallbackPtr; @@ -69,17 +62,12 @@ int virDomainEventCallbackListMarkDelete(virConnectPtr conn, int virDomainEventCallbackListPurgeMarked(virDomainEventCallbackListPtr cbList); +int virDomainEventCallbackListCount(virDomainEventCallbackListPtr cbList); + /** * Dispatching domain events that come in while * in a call / response rpc */ -struct _virDomainEvent { - int id; - char *name; - unsigned char uuid[VIR_UUID_BUFLEN]; - int type; - int detail; -}; typedef struct _virDomainEvent virDomainEvent; typedef virDomainEvent *virDomainEventPtr; diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index 3d10868179..cb71f354f1 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -5021,7 +5021,7 @@ static nsresult vboxCallbackOnMachineStateChange (IVirtualBoxCallback *pThis, dom = vboxDomainLookupByUUID(g_pVBoxGlobalData->conn, uuid); if (dom) { - int i = 0; + virDomainEventPtr ev; if (state == MachineState_Starting) { event = VIR_DOMAIN_EVENT_STARTED; @@ -5052,14 +5052,14 @@ static nsresult vboxCallbackOnMachineStateChange (IVirtualBoxCallback *pThis, detail = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN; } - for (i = 0; i < g_pVBoxGlobalData->domainEventCallbacks->count; i++) { - if (g_pVBoxGlobalData->domainEventCallbacks->callbacks[i]->cb) { - g_pVBoxGlobalData->domainEventCallbacks->callbacks[i]->cb(g_pVBoxGlobalData->conn, - dom, - event, - detail, - NULL); - } + ev = virDomainEventNewFromDom(dom, event, detail); + + if (ev) { + virDomainEventDispatch(ev, + g_pVBoxGlobalData->domainEventCallbacks, + virDomainEventDispatchDefaultFunc, + NULL); + virDomainEventFree(ev); } } } @@ -5142,7 +5142,7 @@ static nsresult vboxCallbackOnMachineRegistered (IVirtualBoxCallback *pThis, dom = vboxDomainLookupByUUID(g_pVBoxGlobalData->conn, uuid); if (dom) { - int i = 0; + virDomainEventPtr ev; /* CURRENT LIMITATION: we never get the VIR_DOMAIN_EVENT_UNDEFINED * event becuase the when the machine is de-registered the call @@ -5158,14 +5158,14 @@ static nsresult vboxCallbackOnMachineRegistered (IVirtualBoxCallback *pThis, detail = VIR_DOMAIN_EVENT_UNDEFINED_REMOVED; } - for (i = 0; i < g_pVBoxGlobalData->domainEventCallbacks->count; i++) { - if (g_pVBoxGlobalData->domainEventCallbacks->callbacks[i]->cb) { - g_pVBoxGlobalData->domainEventCallbacks->callbacks[i]->cb(g_pVBoxGlobalData->conn, - dom, - event, - detail, - NULL); - } + ev = virDomainEventNewFromDom(dom, event, detail); + + if (ev) { + virDomainEventDispatch(ev, + g_pVBoxGlobalData->domainEventCallbacks, + virDomainEventDispatchDefaultFunc, + NULL); + virDomainEventFree(ev); } } } @@ -5412,28 +5412,17 @@ static int vboxDomainEventDeregister (virConnectPtr conn, ret = virDomainEventCallbackListRemove(conn, data->domainEventCallbacks, callback); - if(data->vboxCallback) { + if (data->vboxCallback) { /* check count here of how many times register was called * and only on the last de-register do the un-register call */ - if (data->domainEventCallbacks && (data->domainEventCallbacks->count <= 0)) { - int i = 0; - + if (data->domainEventCallbacks && virDomainEventCallbackListCount(data->domainEventCallbacks) == 0) { data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback); VBOX_RELEASE(data->vboxCallback); /* Remove the Event file handle on which we are listening as well */ virEventRemoveHandle(data->fdWatch); data->fdWatch = -1; - - /* iterate and free all the opaque objects using the - * freecb callback provided in vboxDomainEventRegister() - */ - for (i = 0; i < data->domainEventCallbacks->count; i++) { - if (data->domainEventCallbacks->callbacks[i]->freecb) { - data->domainEventCallbacks->callbacks[i]->freecb(data->domainEventCallbacks->callbacks[i]->opaque); - } - } } }