mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
Add a virFreeCallback to virDomainEventRegister (from David Lively)
This commit is contained in:
parent
8a1553e43a
commit
3d41e86534
13
ChangeLog
13
ChangeLog
@ -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>
|
||||
|
||||
* src/Makefile.am: Fix build on MinGW where linker flags
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -281,7 +281,8 @@ typedef int
|
||||
(*virDrvDomainEventRegister)
|
||||
(virConnectPtr conn,
|
||||
void *callback,
|
||||
void *opaque);
|
||||
void *opaque,
|
||||
virFreeCallback freecb);
|
||||
|
||||
typedef int
|
||||
(*virDrvDomainEventDeregister)
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user