Re: [libvirt] [PATCH] Java bindings for domain events

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

 



On Mon, 2008-11-17 at 22:22 +0000, Daniel P. Berrange wrote:
> On Mon, Nov 17, 2008 at 03:55:13PM -0500, David Lively wrote:

> Functionally this all looks fine.
> 
> From a style point of view, we should keep consistency with the other
> virEventAddHandle func in terms of typing / param ordering. I prefer
> to have a typedef for the 'freefunc', even though its trivial, because
> I hate reading function prototypes :-) Whether we have the freefunc,
> before or after the 'void opaque' in the register method I don't
> really mind one way or the other as long as we're consistent. Having
> the freefunc last is probably best, since its very often just going
> to be NULL.
> 
> Daniel

Ok, here's a version with "virFreeCallback" as the "freefunc" (now
called "freecb") typedef.

Dave

diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c
index 0a741ea..11d62c7 100644
--- a/examples/domain-events/events-c/event-test.c
+++ b/examples/domain-events/events-c/event-test.c
@@ -1,7 +1,9 @@
 #include <config.h>
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 
 #if HAVE_SYS_POLL_H
 #include <sys/types.h>
@@ -168,6 +170,13 @@ int myDomainEventCallback2 (virConnectPtr conn ATTRIBUTE_UNUSED,
     return 0;
 }
 
+static void myFreeFunc(void *opaque)
+{
+    char *str = opaque;
+    printf("%s: Freeing [%s]\n", __FUNCTION__, str);
+    free(str);
+}
+
 
 /* EventImpl Functions */
 int myEventHandleTypeToPollEvent(virEventHandleType events)
@@ -254,15 +263,27 @@ void usage(const char *pname)
     printf("%s uri\n", pname);
 }
 
+int run = 1;
+
+static void stop(int sig)
+{
+    printf("Exiting on signal %d\n", sig);
+    run = 0;
+}
+
+
 int main(int argc, char **argv)
 {
-    int run=1;
     int sts;
+    struct sigaction action_stop = {
+        .sa_handler = stop
+    };
 
     if(argc > 1 && STREQ(argv[1],"--help")) {
         usage(argv[0]);
         return -1;
     }
+
     virEventRegisterImpl( myEventAddHandleFunc,
                           myEventUpdateHandleFunc,
                           myEventRemoveHandleFunc,
@@ -277,11 +298,16 @@ int main(int argc, char **argv)
         return -1;
     }
 
+    sigaction(SIGTERM, &action_stop, NULL);
+    sigaction(SIGINT, &action_stop, NULL);
+
     DEBUG0("Registering domain event cbs");
 
     /* Add 2 callbacks to prove this works with more than just one */
-    virConnectDomainEventRegister(dconn, myDomainEventCallback1, NULL);
-    virConnectDomainEventRegister(dconn, myDomainEventCallback2, NULL);
+    virConnectDomainEventRegister(dconn, myDomainEventCallback1,
+                                  strdup("callback 1"), myFreeFunc);
+    virConnectDomainEventRegister(dconn, myDomainEventCallback2,
+                                  strdup("callback 2"), myFreeFunc);
 
     while(run) {
         struct pollfd pfd = { .fd = h_fd,
@@ -315,9 +341,15 @@ int main(int argc, char **argv)
 
     }
 
+    DEBUG0("Deregistering event handlers");
+    virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
+    virConnectDomainEventDeregister(dconn, myDomainEventCallback2);
+
+    DEBUG0("Closing connection");
     if( dconn && virConnectClose(dconn)<0 ) {
         printf("error closing\n");
     }
+
     printf("done\n");
     return 0;
 }
diff --git a/include/libvirt/libvirt.h b/include/libvirt/libvirt.h
index d1bb154..c1970ab 100644
--- a/include/libvirt/libvirt.h
+++ b/include/libvirt/libvirt.h
@@ -1093,9 +1093,12 @@ typedef int (*virConnectDomainEventCallback)(virConnectPtr conn,
                                              int detail,
                                              void *opaque);
 
+typedef void (*virFreeCallback)(void *opaque);
+
 int virConnectDomainEventRegister(virConnectPtr conn,
                                   virConnectDomainEventCallback cb,
-                                  void *opaque);
+                                  void *opaque,
+                                  virFreeCallback freecb);
 
 int virConnectDomainEventDeregister(virConnectPtr conn,
                                     virConnectDomainEventCallback cb);
diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index 0ee657a..d3e351d 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -1093,9 +1093,12 @@ typedef int (*virConnectDomainEventCallback)(virConnectPtr conn,
                                              int detail,
                                              void *opaque);
 
+typedef void (*virFreeCallback)(void *opaque);
+
 int virConnectDomainEventRegister(virConnectPtr conn,
                                   virConnectDomainEventCallback cb,
-                                  void *opaque);
+                                  void *opaque,
+                                  virFreeCallback freecb);
 
 int virConnectDomainEventDeregister(virConnectPtr conn,
                                     virConnectDomainEventCallback cb);
diff --git a/python/libvir.c b/python/libvir.c
index 96ccb28..3888af5 100644
--- a/python/libvir.c
+++ b/python/libvir.c
@@ -1651,7 +1651,7 @@ libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
 
     ret = virConnectDomainEventRegister(conn,
                                         libvirt_virConnectDomainEventCallback,
-                                        (void *)pyobj_conn_inst);
+                                        (void *)pyobj_conn_inst, NULL);
 
     LIBVIRT_END_ALLOW_THREADS;
 
diff --git a/qemud/remote.c b/qemud/remote.c
index e9b2387..68030ea 100644
--- a/qemud/remote.c
+++ b/qemud/remote.c
@@ -3754,7 +3754,7 @@ remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED
 
     /* Register event delivery callback */
     REMOTE_DEBUG("%s","Registering to relay remote events");
-    virConnectDomainEventRegister(client->conn, remoteRelayDomainEvent, client);
+    virConnectDomainEventRegister(client->conn, remoteRelayDomainEvent, client, NULL);
 
     if(ret)
         ret->cb_registered = 1;
diff --git a/src/domain_event.c b/src/domain_event.c
index 2d15936..85ca9b7 100644
--- a/src/domain_event.c
+++ b/src/domain_event.c
@@ -39,6 +39,9 @@ virDomainEventCallbackListFree(virDomainEventCallbackListPtr list)
 {
     int i;
     for (i=0; i<list->count; i++) {
+        virFreeCallback freecb = list->callbacks[i]->freecb;
+        if (freecb)
+            (*freecb)(list->callbacks[i]->opaque);
         VIR_FREE(list->callbacks[i]);
     }
     VIR_FREE(list);
@@ -60,6 +63,9 @@ virDomainEventCallbackListRemove(virConnectPtr conn,
     for (i = 0 ; i < cbList->count ; i++) {
         if(cbList->callbacks[i]->cb == callback &&
            cbList->callbacks[i]->conn == conn) {
+            virFreeCallback freecb = cbList->callbacks[i]->freecb;
+            if (freecb)
+                (*freecb)(cbList->callbacks[i]->opaque);
             virUnrefConnect(cbList->callbacks[i]->conn);
             VIR_FREE(cbList->callbacks[i]);
 
@@ -94,7 +100,8 @@ int
 virDomainEventCallbackListAdd(virConnectPtr conn,
                               virDomainEventCallbackListPtr cbList,
                               virConnectDomainEventCallback callback,
-                              void *opaque)
+                              void *opaque,
+                              virFreeCallback freecb)
 {
     virDomainEventCallbackPtr event;
     int n;
@@ -120,6 +127,7 @@ virDomainEventCallbackListAdd(virConnectPtr conn,
     event->conn = conn;
     event->cb = callback;
     event->opaque = opaque;
+    event->freecb = freecb;
 
     /* Make space on list */
     n = cbList->count;
diff --git a/src/domain_event.h b/src/domain_event.h
index c26ec3b..cfec1e1 100644
--- a/src/domain_event.h
+++ b/src/domain_event.h
@@ -30,6 +30,8 @@ struct _virDomainEventCallback {
     virConnectPtr conn;
     virConnectDomainEventCallback cb;
     void *opaque;
+    virFreeCallback freecb;
+
 };
 typedef struct _virDomainEventCallback virDomainEventCallback;
 typedef virDomainEventCallback *virDomainEventCallbackPtr;
@@ -46,7 +48,8 @@ void virDomainEventCallbackListFree(virDomainEventCallbackListPtr list);
 int virDomainEventCallbackListAdd(virConnectPtr conn,
                                   virDomainEventCallbackListPtr cbList,
                                   virConnectDomainEventCallback callback,
-                                  void *opaque);
+                                  void *opaque,
+                                  virFreeCallback freecb);
 
 int virDomainEventCallbackListRemove(virConnectPtr conn,
                                      virDomainEventCallbackListPtr cbList,
diff --git a/src/driver.h b/src/driver.h
index 98a2763..8962aff 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -281,7 +281,8 @@ typedef int
     (*virDrvDomainEventRegister)
                     (virConnectPtr conn,
                      void *callback,
-                     void *opaque);
+                     void *opaque,
+                     virFreeCallback freecb);
 
 typedef int
     (*virDrvDomainEventDeregister)
diff --git a/src/libvirt.c b/src/libvirt.c
index f257c0f..a1edb69 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -5372,7 +5372,8 @@ virStorageVolGetPath(virStorageVolPtr vol)
 int
 virConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback cb,
-                              void *opaque)
+                              void *opaque,
+                              virFreeCallback freecb)
 {
 
     if (!VIR_IS_CONNECT(conn)) {
@@ -5385,7 +5386,7 @@ virConnectDomainEventRegister(virConnectPtr conn,
     }
 
     if ((conn->driver) && (conn->driver->domainEventRegister))
-        return conn->driver->domainEventRegister (conn, cb, opaque);
+        return conn->driver->domainEventRegister (conn, cb, opaque, freecb);
     return -1;
 }
 
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index f9e93f8..641690e 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -3384,12 +3384,13 @@ done:
 static int
 qemudDomainEventRegister (virConnectPtr conn,
                           void *callback,
-                          void *opaque)
+                          void *opaque,
+                          virFreeCallback freecb)
 {
     struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
 
     return virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
-                                         callback, opaque);
+                                         callback, opaque, freecb);
 }
 
 static int
diff --git a/src/remote_internal.c b/src/remote_internal.c
index 2ca7930..349aea4 100644
--- a/src/remote_internal.c
+++ b/src/remote_internal.c
@@ -4474,13 +4474,14 @@ remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
 /*----------------------------------------------------------------------*/
 
 static int remoteDomainEventRegister (virConnectPtr conn,
-                               void *callback ATTRIBUTE_UNUSED,
-                               void *opaque ATTRIBUTE_UNUSED)
+                                      void *callback ATTRIBUTE_UNUSED,
+                                      void *opaque ATTRIBUTE_UNUSED,
+				      virFreeCallback freecb)
 {
     struct private_data *priv = conn->privateData;
 
     if (virDomainEventCallbackListAdd(conn, priv->callbackList,
-                                  callback, opaque) < 0) {
+				      callback, opaque, freecb) < 0) {
          error (conn, VIR_ERR_RPC, _("adding cb to list"));
          return -1;
     }
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]