Add a virFreeCallback to virDomainEventRegister (from David Lively)

This commit is contained in:
Daniel P. Berrange 2008-11-19 15:25:24 +00:00
parent 8a1553e43a
commit 3d41e86534
12 changed files with 83 additions and 17 deletions

View File

@ -1,3 +1,16 @@
Wed Nov 19 15:25:00 GMT 2008 Daniel Berrange <berrange@redhat.com>
Add a callback for freeing the user data for callbacks
(patch from David Lively)
* include/libvirt/libvirt.h.in: Add a virFreeCallback type
and add it to virDomainEventRegister
* qemud/remote.c, src/domain_event.h, src/domain_event.c,
src/driver.h, src/qemu_driver.c, src/remote_internal.c,
python/libvirt.c: Update to call the virFreeCallback where
needed
* examples/domain-events/events-c/event-test.c: use the
virFreeCallback for releasing data
Mon Nov 18 12:24:00 GMT 2008 Daniel Berrange <berrange@redhat.com> Mon Nov 18 12:24:00 GMT 2008 Daniel Berrange <berrange@redhat.com>
* src/Makefile.am: Fix build on MinGW where linker flags * src/Makefile.am: Fix build on MinGW where linker flags

View File

@ -1,7 +1,9 @@
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#if HAVE_SYS_POLL_H #if HAVE_SYS_POLL_H
#include <sys/types.h> #include <sys/types.h>
@ -168,6 +170,13 @@ int myDomainEventCallback2 (virConnectPtr conn ATTRIBUTE_UNUSED,
return 0; return 0;
} }
static void myFreeFunc(void *opaque)
{
char *str = opaque;
printf("%s: Freeing [%s]\n", __FUNCTION__, str);
free(str);
}
/* EventImpl Functions */ /* EventImpl Functions */
int myEventHandleTypeToPollEvent(virEventHandleType events) int myEventHandleTypeToPollEvent(virEventHandleType events)
@ -254,15 +263,27 @@ void usage(const char *pname)
printf("%s uri\n", 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 main(int argc, char **argv)
{ {
int run=1;
int sts; int sts;
struct sigaction action_stop = {
.sa_handler = stop
};
if(argc > 1 && STREQ(argv[1],"--help")) { if(argc > 1 && STREQ(argv[1],"--help")) {
usage(argv[0]); usage(argv[0]);
return -1; return -1;
} }
virEventRegisterImpl( myEventAddHandleFunc, virEventRegisterImpl( myEventAddHandleFunc,
myEventUpdateHandleFunc, myEventUpdateHandleFunc,
myEventRemoveHandleFunc, myEventRemoveHandleFunc,
@ -277,11 +298,16 @@ int main(int argc, char **argv)
return -1; return -1;
} }
sigaction(SIGTERM, &action_stop, NULL);
sigaction(SIGINT, &action_stop, NULL);
DEBUG0("Registering domain event cbs"); DEBUG0("Registering domain event cbs");
/* Add 2 callbacks to prove this works with more than just one */ /* Add 2 callbacks to prove this works with more than just one */
virConnectDomainEventRegister(dconn, myDomainEventCallback1, NULL); virConnectDomainEventRegister(dconn, myDomainEventCallback1,
virConnectDomainEventRegister(dconn, myDomainEventCallback2, NULL); strdup("callback 1"), myFreeFunc);
virConnectDomainEventRegister(dconn, myDomainEventCallback2,
strdup("callback 2"), myFreeFunc);
while(run) { while(run) {
struct pollfd pfd = { .fd = h_fd, 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 ) { if( dconn && virConnectClose(dconn)<0 ) {
printf("error closing\n"); printf("error closing\n");
} }
printf("done\n"); printf("done\n");
return 0; return 0;
} }

View File

@ -1093,9 +1093,12 @@ typedef int (*virConnectDomainEventCallback)(virConnectPtr conn,
int detail, int detail,
void *opaque); void *opaque);
typedef void (*virFreeCallback)(void *opaque);
int virConnectDomainEventRegister(virConnectPtr conn, int virConnectDomainEventRegister(virConnectPtr conn,
virConnectDomainEventCallback cb, virConnectDomainEventCallback cb,
void *opaque); void *opaque,
virFreeCallback freecb);
int virConnectDomainEventDeregister(virConnectPtr conn, int virConnectDomainEventDeregister(virConnectPtr conn,
virConnectDomainEventCallback cb); virConnectDomainEventCallback cb);

View File

@ -1093,9 +1093,12 @@ typedef int (*virConnectDomainEventCallback)(virConnectPtr conn,
int detail, int detail,
void *opaque); void *opaque);
typedef void (*virFreeCallback)(void *opaque);
int virConnectDomainEventRegister(virConnectPtr conn, int virConnectDomainEventRegister(virConnectPtr conn,
virConnectDomainEventCallback cb, virConnectDomainEventCallback cb,
void *opaque); void *opaque,
virFreeCallback freecb);
int virConnectDomainEventDeregister(virConnectPtr conn, int virConnectDomainEventDeregister(virConnectPtr conn,
virConnectDomainEventCallback cb); virConnectDomainEventCallback cb);

View File

@ -1651,7 +1651,7 @@ libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
ret = virConnectDomainEventRegister(conn, ret = virConnectDomainEventRegister(conn,
libvirt_virConnectDomainEventCallback, libvirt_virConnectDomainEventCallback,
(void *)pyobj_conn_inst); (void *)pyobj_conn_inst, NULL);
LIBVIRT_END_ALLOW_THREADS; LIBVIRT_END_ALLOW_THREADS;

View File

@ -3754,7 +3754,7 @@ remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED
/* Register event delivery callback */ /* Register event delivery callback */
REMOTE_DEBUG("%s","Registering to relay remote events"); REMOTE_DEBUG("%s","Registering to relay remote events");
virConnectDomainEventRegister(client->conn, remoteRelayDomainEvent, client); virConnectDomainEventRegister(client->conn, remoteRelayDomainEvent, client, NULL);
if(ret) if(ret)
ret->cb_registered = 1; ret->cb_registered = 1;

View File

@ -39,6 +39,9 @@ virDomainEventCallbackListFree(virDomainEventCallbackListPtr list)
{ {
int i; int i;
for (i=0; i<list->count; 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->callbacks[i]);
} }
VIR_FREE(list); VIR_FREE(list);
@ -60,6 +63,9 @@ virDomainEventCallbackListRemove(virConnectPtr conn,
for (i = 0 ; i < cbList->count ; i++) { for (i = 0 ; i < cbList->count ; i++) {
if(cbList->callbacks[i]->cb == callback && if(cbList->callbacks[i]->cb == callback &&
cbList->callbacks[i]->conn == conn) { cbList->callbacks[i]->conn == conn) {
virFreeCallback freecb = cbList->callbacks[i]->freecb;
if (freecb)
(*freecb)(cbList->callbacks[i]->opaque);
virUnrefConnect(cbList->callbacks[i]->conn); virUnrefConnect(cbList->callbacks[i]->conn);
VIR_FREE(cbList->callbacks[i]); VIR_FREE(cbList->callbacks[i]);
@ -94,7 +100,8 @@ int
virDomainEventCallbackListAdd(virConnectPtr conn, virDomainEventCallbackListAdd(virConnectPtr conn,
virDomainEventCallbackListPtr cbList, virDomainEventCallbackListPtr cbList,
virConnectDomainEventCallback callback, virConnectDomainEventCallback callback,
void *opaque) void *opaque,
virFreeCallback freecb)
{ {
virDomainEventCallbackPtr event; virDomainEventCallbackPtr event;
int n; int n;
@ -120,6 +127,7 @@ virDomainEventCallbackListAdd(virConnectPtr conn,
event->conn = conn; event->conn = conn;
event->cb = callback; event->cb = callback;
event->opaque = opaque; event->opaque = opaque;
event->freecb = freecb;
/* Make space on list */ /* Make space on list */
n = cbList->count; n = cbList->count;

View File

@ -30,6 +30,8 @@ struct _virDomainEventCallback {
virConnectPtr conn; virConnectPtr conn;
virConnectDomainEventCallback cb; virConnectDomainEventCallback cb;
void *opaque; void *opaque;
virFreeCallback freecb;
}; };
typedef struct _virDomainEventCallback virDomainEventCallback; typedef struct _virDomainEventCallback virDomainEventCallback;
typedef virDomainEventCallback *virDomainEventCallbackPtr; typedef virDomainEventCallback *virDomainEventCallbackPtr;
@ -46,7 +48,8 @@ void virDomainEventCallbackListFree(virDomainEventCallbackListPtr list);
int virDomainEventCallbackListAdd(virConnectPtr conn, int virDomainEventCallbackListAdd(virConnectPtr conn,
virDomainEventCallbackListPtr cbList, virDomainEventCallbackListPtr cbList,
virConnectDomainEventCallback callback, virConnectDomainEventCallback callback,
void *opaque); void *opaque,
virFreeCallback freecb);
int virDomainEventCallbackListRemove(virConnectPtr conn, int virDomainEventCallbackListRemove(virConnectPtr conn,
virDomainEventCallbackListPtr cbList, virDomainEventCallbackListPtr cbList,

View File

@ -281,7 +281,8 @@ typedef int
(*virDrvDomainEventRegister) (*virDrvDomainEventRegister)
(virConnectPtr conn, (virConnectPtr conn,
void *callback, void *callback,
void *opaque); void *opaque,
virFreeCallback freecb);
typedef int typedef int
(*virDrvDomainEventDeregister) (*virDrvDomainEventDeregister)

View File

@ -5372,7 +5372,8 @@ virStorageVolGetPath(virStorageVolPtr vol)
int int
virConnectDomainEventRegister(virConnectPtr conn, virConnectDomainEventRegister(virConnectPtr conn,
virConnectDomainEventCallback cb, virConnectDomainEventCallback cb,
void *opaque) void *opaque,
virFreeCallback freecb)
{ {
if (!VIR_IS_CONNECT(conn)) { if (!VIR_IS_CONNECT(conn)) {
@ -5385,7 +5386,7 @@ virConnectDomainEventRegister(virConnectPtr conn,
} }
if ((conn->driver) && (conn->driver->domainEventRegister)) if ((conn->driver) && (conn->driver->domainEventRegister))
return conn->driver->domainEventRegister (conn, cb, opaque); return conn->driver->domainEventRegister (conn, cb, opaque, freecb);
return -1; return -1;
} }

View File

@ -3384,12 +3384,13 @@ done:
static int static int
qemudDomainEventRegister (virConnectPtr conn, qemudDomainEventRegister (virConnectPtr conn,
void *callback, void *callback,
void *opaque) void *opaque,
virFreeCallback freecb)
{ {
struct qemud_driver *driver = (struct qemud_driver *)conn->privateData; struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
return virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks, return virDomainEventCallbackListAdd(conn, driver->domainEventCallbacks,
callback, opaque); callback, opaque, freecb);
} }
static int static int

View File

@ -4474,13 +4474,14 @@ remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
/*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/
static int remoteDomainEventRegister (virConnectPtr conn, static int remoteDomainEventRegister (virConnectPtr conn,
void *callback ATTRIBUTE_UNUSED, void *callback ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED) void *opaque ATTRIBUTE_UNUSED,
virFreeCallback freecb)
{ {
struct private_data *priv = conn->privateData; struct private_data *priv = conn->privateData;
if (virDomainEventCallbackListAdd(conn, priv->callbackList, if (virDomainEventCallbackListAdd(conn, priv->callbackList,
callback, opaque) < 0) { callback, opaque, freecb) < 0) {
error (conn, VIR_ERR_RPC, _("adding cb to list")); error (conn, VIR_ERR_RPC, _("adding cb to list"));
return -1; return -1;
} }