[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 02/13] 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 e791cab..b520232 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 8d1b330..5d8faee 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 b540f61..e4a7748 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -5015,7 +5015,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;
@@ -5046,14 +5046,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);
             }
         }
     }
@@ -5136,7 +5136,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
@@ -5152,14 +5152,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);
             }
         }
     }
@@ -5406,28 +5406,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);
-                }
-            }
         }
     }
 
-- 
1.6.6.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]