[libvirt] [PATCH 3/5] conf: implementing some functions to generate volume events.

Julio Faracco jcfaracco at gmail.com
Sun May 27 19:25:37 UTC 2018


This commit defines some functions to create new events for volumes
according the events available for volumes.

Signed-off-by: Julio Faracco <jcfaracco at gmail.com>
---
 src/conf/storage_event.c | 155 +++++++++++++++++++++++++++++++++++++++
 src/conf/storage_event.h |  19 +++++
 src/libvirt_private.syms |   2 +
 3 files changed, 176 insertions(+)

diff --git a/src/conf/storage_event.c b/src/conf/storage_event.c
index 32a12eb63f..8d77b2a6cb 100644
--- a/src/conf/storage_event.c
+++ b/src/conf/storage_event.c
@@ -56,6 +56,24 @@ struct _virStoragePoolEventRefresh {
 typedef struct _virStoragePoolEventRefresh virStoragePoolEventRefresh;
 typedef virStoragePoolEventRefresh *virStoragePoolEventRefreshPtr;
 
+struct _virStorageVolEvent {
+    virObjectEvent parent;
+
+    /* Unused attribute to allow for subclass creation */
+    bool dummy;
+};
+typedef struct _virStorageVolEvent virStorageVolEvent;
+typedef virStorageVolEvent *virStorageVolEventPtr;
+
+struct _virStorageVolEventLifecycle {
+    virStorageVolEvent parent;
+
+    int type;
+    int detail;
+};
+typedef struct _virStorageVolEventLifecycle virStorageVolEventLifecycle;
+typedef virStorageVolEventLifecycle *virStorageVolEventLifecyclePtr;
+
 static virClassPtr virStoragePoolEventClass;
 static virClassPtr virStoragePoolEventLifecycleClass;
 static virClassPtr virStoragePoolEventRefreshClass;
@@ -63,6 +81,11 @@ static void virStoragePoolEventDispose(void *obj);
 static void virStoragePoolEventLifecycleDispose(void *obj);
 static void virStoragePoolEventRefreshDispose(void *obj);
 
+static virClassPtr virStorageVolEventClass;
+static virClassPtr virStorageVolEventLifecycleClass;
+static void virStorageVolEventDispose(void *obj);
+static void virStorageVolEventLifecycleDispose(void *obj);
+
 static int
 virStoragePoolEventsOnceInit(void)
 {
@@ -78,7 +101,20 @@ virStoragePoolEventsOnceInit(void)
     return 0;
 }
 
+static int
+virStorageVolEventsOnceInit(void)
+{
+    if (!VIR_CLASS_NEW(virStorageVolEvent, virClassForObjectEvent()))
+        return -1;
+
+    if (!VIR_CLASS_NEW(virStorageVolEventLifecycle, virStorageVolEventClass))
+        return -1;
+
+    return 0;
+}
+
 VIR_ONCE_GLOBAL_INIT(virStoragePoolEvents)
+VIR_ONCE_GLOBAL_INIT(virStorageVolEvents)
 
 static void
 virStoragePoolEventDispose(void *obj)
@@ -103,6 +139,20 @@ virStoragePoolEventRefreshDispose(void *obj)
     VIR_DEBUG("obj=%p", event);
 }
 
+static void
+virStorageVolEventDispose(void *obj)
+{
+    virStorageVolEventPtr event = obj;
+    VIR_DEBUG("obj=%p", event);
+}
+
+
+static void
+virStorageVolEventLifecycleDispose(void *obj)
+{
+    virStorageVolEventLifecyclePtr event = obj;
+    VIR_DEBUG("obj=%p", event);
+}
 
 static void
 virStoragePoolEventDispatchDefaultFunc(virConnectPtr conn,
@@ -147,6 +197,40 @@ virStoragePoolEventDispatchDefaultFunc(virConnectPtr conn,
 }
 
 
+static void
+virStorageVolEventDispatchDefaultFunc(virConnectPtr conn,
+                                      virObjectEventPtr event,
+                                      virConnectObjectEventGenericCallback cb ATTRIBUTE_UNUSED,
+                                      void *cbopaque ATTRIBUTE_UNUSED)
+{
+    virStorageVolPtr vol = virStorageVolLookupByKey(conn,
+                                                    event->meta.key);
+    if (!vol)
+        return;
+
+    switch ((virStorageVolEventID)event->eventID) {
+    case VIR_STORAGE_VOL_EVENT_ID_LIFECYCLE:
+        {
+            virStorageVolEventLifecyclePtr storageVolLifecycleEvent;
+
+            storageVolLifecycleEvent = (virStorageVolEventLifecyclePtr)event;
+            ((virConnectStorageVolEventLifecycleCallback)cb)(conn, vol,
+                                                             storageVolLifecycleEvent->type,
+                                                             storageVolLifecycleEvent->detail,
+                                                             cbopaque);
+            goto cleanup;
+        }
+
+    case VIR_STORAGE_VOL_EVENT_ID_LAST:
+        break;
+    }
+    VIR_WARN("Unexpected event ID %d", event->eventID);
+
+ cleanup:
+    virObjectUnref(vol);
+}
+
+
 /**
  * virStoragePoolEventStateRegisterID:
  * @conn: connection to associate with callback
@@ -189,6 +273,44 @@ virStoragePoolEventStateRegisterID(virConnectPtr conn,
                                          false, callbackID, false);
 }
 
+/**
+ * virStorageVolEventStateRegisterID:
+ * @conn: connection to associate with callback
+ * @state: object event state
+ * @vol: storage vol to filter on or NULL for all storage volumes
+ * @eventID: ID of the event type to register for
+ * @cb: function to invoke when event occurs
+ * @opaque: data blob to pass to @callback
+ * @freecb: callback to free @opaque
+ * @callbackID: filled with callback ID
+ *
+ * Register the function @cb with connection @conn, from @state, for
+ * events of type @eventID, and return the registration handle in
+ * @callbackID.
+ *
+ * Returns: the number of callbacks now registered, or -1 on error
+ */
+int
+virStorageVolEventStateRegisterID(virConnectPtr conn,
+                                  virObjectEventStatePtr state,
+                                  virStorageVolPtr vol,
+                                  int eventID,
+                                  virConnectStorageVolEventGenericCallback cb,
+                                  void *opaque,
+                                  virFreeCallback freecb,
+                                  int *callbackID)
+{
+    if (virStorageVolEventsInitialize() < 0)
+        return -1;
+
+    return virObjectEventStateRegisterID(conn, state, vol ? vol->key : NULL,
+                                         NULL, NULL,
+                                         virStorageVolEventClass, eventID,
+                                         VIR_OBJECT_EVENT_CALLBACK(cb),
+                                         opaque, freecb,
+                                         false, callbackID, false);
+}
+
 
 /**
  * virStoragePoolEventStateRegisterClient:
@@ -268,6 +390,39 @@ virStoragePoolEventLifecycleNew(const char *name,
     return (virObjectEventPtr)event;
 }
 
+/**
+ * virStorageVolEventLifecycleNew:
+ * @name: name of the storage volume object the event describes
+ * @key: key of the storage volume object the event describes
+ * @type: type of lifecycle event
+ * @detail: more details about @type
+ *
+ * Create a new storage volume lifecycle event.
+ */
+virObjectEventPtr
+virStorageVolEventLifecycleNew(const char *pool,
+                               const char *name,
+                               const unsigned char *key,
+                               int type,
+                               int detail)
+{
+    virStorageVolEventLifecyclePtr event;
+
+    if (virStorageVolEventsInitialize() < 0)
+        return NULL;
+
+    if (!(event = virObjectEventNew(virStorageVolEventLifecycleClass,
+                                    virStorageVolEventDispatchDefaultFunc,
+                                    VIR_STORAGE_VOL_EVENT_ID_LIFECYCLE,
+                                    0, name, key, pool)))
+        return NULL;
+
+    event->type = type;
+    event->detail = detail;
+
+    return (virObjectEventPtr)event;
+}
+
 
 /**
  * virStoragePoolEventRefreshNew:
diff --git a/src/conf/storage_event.h b/src/conf/storage_event.h
index ea726911fa..59adf622d7 100644
--- a/src/conf/storage_event.h
+++ b/src/conf/storage_event.h
@@ -39,6 +39,18 @@ virStoragePoolEventStateRegisterID(virConnectPtr conn,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5)
     ATTRIBUTE_NONNULL(8);
 
+int
+virStorageVolEventStateRegisterID(virConnectPtr conn,
+                                  virObjectEventStatePtr state,
+                                  virStorageVolPtr vol,
+                                  int eventID,
+                                  virConnectStorageVolEventGenericCallback cb,
+                                  void *opaque,
+                                  virFreeCallback freecb,
+                                  int *callbackID)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(5)
+    ATTRIBUTE_NONNULL(8);
+
 int
 virStoragePoolEventStateRegisterClient(virConnectPtr conn,
                                    virObjectEventStatePtr state,
@@ -61,4 +73,11 @@ virObjectEventPtr
 virStoragePoolEventRefreshNew(const char *name,
                               const unsigned char *uuid);
 
+virObjectEventPtr
+virStorageVolEventLifecycleNew(const char *pool,
+                               const char *name,
+                               const unsigned char *key,
+                               int type,
+                               int detail);
+
 #endif
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a97b7fe223..4e62a02c02 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -940,6 +940,8 @@ virStorageVolTypeToString;
 virStoragePoolEventLifecycleNew;
 virStoragePoolEventRefreshNew;
 virStoragePoolEventStateRegisterID;
+virStorageVolEventLifecycleNew;
+virStorageVolEventStateRegisterID;
 
 
 # conf/virchrdev.h
-- 
2.17.0




More information about the libvir-list mailing list