From 25905a78f27a3d7d0df3b1ced2d9450d32c9f382 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 26 Jun 2007 22:51:01 +0000 Subject: [PATCH] Switch to using internal event API for QEMU driver --- ChangeLog | 8 ++++ docs/apibuild.py | 2 + qemud/driver.c | 3 ++ qemud/event.c | 8 ++-- qemud/event.h | 35 ++++---------- qemud/qemud.c | 48 +++++++++---------- src/Makefile.am | 1 + src/event.c | 80 ++++++++++++++++++++++++++++++++ src/event.h | 100 ++++++++++++++++++++++++++++++++++++++++ src/libvirt_sym.version | 8 ++++ 10 files changed, 239 insertions(+), 54 deletions(-) create mode 100644 src/event.c create mode 100644 src/event.h diff --git a/ChangeLog b/ChangeLog index f27158f3d8..d75c37ca86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Tue Jun 26 18:47:00 EST 2007 Daniel P. Berrange + + * src/event.h, src/event.c, src/Makefile.am, src/libvirt_sym.version: + Provide an internal API for drivers to register callbacks for + monitoring file handles & generating timer notifications. + * qemud/driver.c, qemud/event.c, qemud/event.h, qemud/qemud.c: + Adapt to make use of internal driver API for events. + Tue Jun 26 18:41:00 EST 2007 Daniel P. Berrange * qemud/conf.c, qemud/conf.h, qemud/dispatch.c, qemud/driver.c, diff --git a/docs/apibuild.py b/docs/apibuild.py index f14221dfe4..dd7670495a 100755 --- a/docs/apibuild.py +++ b/docs/apibuild.py @@ -49,6 +49,8 @@ ignored_files = { "conf.c": "internal code", "console.h": "internal code", "console.c": "internal code", + "event.h": "internal code", + "event.c": "internal code", } ignored_words = { diff --git a/qemud/driver.c b/qemud/driver.c index 00b30a4594..7f0d113e16 100644 --- a/qemud/driver.c +++ b/qemud/driver.c @@ -81,6 +81,9 @@ static int qemudSetNonBlock(int fd) { } +#define virEventAddHandle(fd, events, cb, opaque) virEventAddHandleImpl(fd, events, cb, opaque) +#define virEventRemoveHandle(fd) virEventRemoveHandleImpl(fd) + static void qemudDispatchVMEvent(int fd, int events, void *opaque); int qemudStartVMDaemon(struct qemud_driver *driver, diff --git a/qemud/event.c b/qemud/event.c index 64d7bc4823..6eb41dee12 100644 --- a/qemud/event.c +++ b/qemud/event.c @@ -76,7 +76,7 @@ static int nextTimer = 0; * NB, it *must* be safe to call this from within a callback * For this reason we only ever append to existing list. */ -int virEventAddHandle(int fd, int events, virEventHandleCallback cb, void *opaque) { +int virEventAddHandleImpl(int fd, int events, virEventHandleCallback cb, void *opaque) { qemudDebug("Add handle %d %d %p %p\n", fd, events, cb, opaque); if (eventLoop.handlesCount == eventLoop.handlesAlloc) { struct virEventHandle *tmp; @@ -109,7 +109,7 @@ int virEventAddHandle(int fd, int events, virEventHandleCallback cb, void *opaqu * For this reason we only ever set a flag in the existing list. * Actual deletion will be done out-of-band */ -int virEventRemoveHandle(int fd) { +int virEventRemoveHandleImpl(int fd) { int i; qemudDebug("Remove handle %d\n", fd); for (i = 0 ; i < eventLoop.handlesCount ; i++) { @@ -131,7 +131,7 @@ int virEventRemoveHandle(int fd) { * NB, it *must* be safe to call this from within a callback * For this reason we only ever append to existing list. */ -int virEventAddTimeout(int timeout, virEventTimeoutCallback cb, void *opaque) { +int virEventAddTimeoutImpl(int timeout, virEventTimeoutCallback cb, void *opaque) { struct timeval tv; qemudDebug("Adding timeout with %d ms period", timeout); if (gettimeofday(&tv, NULL) < 0) { @@ -173,7 +173,7 @@ int virEventAddTimeout(int timeout, virEventTimeoutCallback cb, void *opaque) { * For this reason we only ever set a flag in the existing list. * Actual deletion will be done out-of-band */ -int virEventRemoveTimeout(int timer) { +int virEventRemoveTimeoutImpl(int timer) { int i; for (i = 0 ; i < eventLoop.timeoutsCount ; i++) { if (eventLoop.timeouts[i].deleted) diff --git a/qemud/event.h b/qemud/event.h index 671ba50ea5..48f405dd73 100644 --- a/qemud/event.h +++ b/qemud/event.h @@ -24,18 +24,10 @@ #ifndef __VIRTD_EVENT_H__ #define __VIRTD_EVENT_H__ +#include "../src/event.h" /** - * virEventHandleCallback: callback for receiving file handle events - * - * @fd: file handle on which the event occured - * @events: bitset of events from POLLnnn constants - * @opaque: user data registered with handle - */ -typedef void (*virEventHandleCallback)(int fd, int events, void *opaque); - -/** - * virEventAddHandle: register a callback for monitoring file handle events + * virEventAddHandleImpl: register a callback for monitoring file handle events * * @fd: file handle to monitor for events * @events: bitset of events to wach from POLLnnn constants @@ -44,27 +36,19 @@ typedef void (*virEventHandleCallback)(int fd, int events, void *opaque); * * returns -1 if the file handle cannot be registered, 0 upon success */ -int virEventAddHandle(int fd, int events, virEventHandleCallback cb, void *opaque); +int virEventAddHandleImpl(int fd, int events, virEventHandleCallback cb, void *opaque); /** - * virEventRemoveHandle: unregister a callback from a file handle + * virEventRemoveHandleImpl: unregister a callback from a file handle * * @fd: file handle to stop monitoring for events * * returns -1 if the file handle was not registered, 0 upon success */ -int virEventRemoveHandle(int fd); +int virEventRemoveHandleImpl(int fd); /** - * virEventTimeoutCallback: callback for receiving timer events - * - * @timer: timer id emitting the event - * @opaque: user data registered with handle - */ -typedef void (*virEventTimeoutCallback)(int timer, void *opaque); - -/** - * virEventAddTimeout: register a callback for a timer event + * virEventAddTimeoutImpl: register a callback for a timer event * * @timeout: timeout between events in milliseconds * @cb: callback to invoke when an event occurrs @@ -73,17 +57,16 @@ typedef void (*virEventTimeoutCallback)(int timer, void *opaque); * returns -1 if the file handle cannot be registered, a positive * integer timer id upon success */ -int virEventAddTimeout(int timeout, virEventTimeoutCallback cb, void *opaque); +int virEventAddTimeoutImpl(int timeout, virEventTimeoutCallback cb, void *opaque); /** - * virEventRemoveTimeout: unregister a callback for a timer + * virEventRemoveTimeoutImpl: unregister a callback for a timer * * @timer: the timer id to remove * * returns -1 if the timer was not registered, 0 upon success */ -int virEventRemoveTimeout(int timer); - +int virEventRemoveTimeoutImpl(int timer); /** * virEventRunOnce: run a single iteration of the event loop. diff --git a/qemud/qemud.c b/qemud/qemud.c index 03b9210dd2..669c61cf92 100644 --- a/qemud/qemud.c +++ b/qemud/qemud.c @@ -471,10 +471,10 @@ static int qemudListenUnix(struct qemud_server *server, goto cleanup; } - if (virEventAddHandle(sock->fd, - POLLIN| POLLERR | POLLHUP, - qemudDispatchServerEvent, - server) < 0) { + if (virEventAddHandleImpl(sock->fd, + POLLIN| POLLERR | POLLHUP, + qemudDispatchServerEvent, + server) < 0) { qemudLog(QEMUD_ERR, "Failed to add server event callback"); goto cleanup; } @@ -584,10 +584,10 @@ remoteListenTCP (struct qemud_server *server, return -1; } - if (virEventAddHandle(sock->fd, - POLLIN| POLLERR | POLLHUP, - qemudDispatchServerEvent, - server) < 0) { + if (virEventAddHandleImpl(sock->fd, + POLLIN| POLLERR | POLLHUP, + qemudDispatchServerEvent, + server) < 0) { qemudLog(QEMUD_ERR, "Failed to add server event callback"); return -1; } @@ -1056,7 +1056,7 @@ static void qemudDispatchClientFailure(struct qemud_server *server, struct qemud tmp = tmp->next; } - virEventRemoveHandle(client->fd); + virEventRemoveHandleImpl(client->fd); if (client->tls && client->session) gnutls_deinit (client->session); close(client->fd); @@ -1418,22 +1418,22 @@ static int qemudRegisterClientEvent(struct qemud_server *server, struct qemud_client *client, int removeFirst) { if (removeFirst) - if (virEventRemoveHandle(client->fd) < 0) + if (virEventRemoveHandleImpl(client->fd) < 0) return -1; if (client->tls) { - if (virEventAddHandle(client->fd, - (client->direction ? - POLLOUT : POLLIN) | POLLERR | POLLHUP, - qemudDispatchClientEvent, - server) < 0) + if (virEventAddHandleImpl(client->fd, + (client->direction ? + POLLOUT : POLLIN) | POLLERR | POLLHUP, + qemudDispatchClientEvent, + server) < 0) return -1; } else { - if (virEventAddHandle(client->fd, - (client->mode == QEMUD_MODE_TX_PACKET ? - POLLOUT : POLLIN) | POLLERR | POLLHUP, - qemudDispatchClientEvent, - server) < 0) + if (virEventAddHandleImpl(client->fd, + (client->mode == QEMUD_MODE_TX_PACKET ? + POLLOUT : POLLIN) | POLLERR | POLLHUP, + qemudDispatchClientEvent, + server) < 0) return -1; } @@ -1864,10 +1864,10 @@ int main(int argc, char **argv) { goto error2; } - if (virEventAddHandle(sigpipe[0], - POLLIN, - qemudDispatchSignalEvent, - server) < 0) { + if (virEventAddHandleImpl(sigpipe[0], + POLLIN, + qemudDispatchSignalEvent, + server) < 0) { qemudLog(QEMUD_ERR, "Failed to register callback for signal pipe"); ret = 3; goto error2; diff --git a/src/Makefile.am b/src/Makefile.am index c86736c848..26fd46c052 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,6 +31,7 @@ CLIENT_SOURCES = \ test.c test.h \ buf.c buf.h \ xml.c xml.h \ + event.c event.h \ xen_unified.c xen_unified.h \ xen_internal.c xen_internal.h \ xs_internal.c xs_internal.h \ diff --git a/src/event.c b/src/event.c new file mode 100644 index 0000000000..a69ce6bffc --- /dev/null +++ b/src/event.c @@ -0,0 +1,80 @@ +/* + * event.h: event loop for monitoring file handles + * + * Copyright (C) 2007 Daniel P. Berrange + * Copyright (C) 2007 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Daniel P. Berrange + */ + + +#include "event.h" + +#include + +static virEventAddHandleFunc addHandleImpl = NULL; +static virEventRemoveHandleFunc removeHandleImpl = NULL; +static virEventAddTimeoutFunc addTimeoutImpl = NULL; +static virEventRemoveTimeoutFunc removeTimeoutImpl = NULL; + +int virEventAddHandle(int fd, int events, virEventHandleCallback cb, void *opaque) { + if (!addHandleImpl) + return -1; + + return addHandleImpl(fd, events, cb, opaque); +} + +int virEventRemoveHandle(int fd) { + if (!removeHandleImpl) + return -1; + + return removeHandleImpl(fd); +} + +int virEventAddTimeout(int timeout, virEventTimeoutCallback cb, void *opaque) { + if (!addTimeoutImpl) + return -1; + + return addTimeoutImpl(timeout, cb, opaque); +} + +int virEventRemoveTimeout(int timer) { + if (!removeTimeoutImpl) + return -1; + + return removeTimeoutImpl(timer); +} + +void __virEventRegisterImpl(virEventAddHandleFunc addHandle, + virEventRemoveHandleFunc removeHandle, + virEventAddTimeoutFunc addTimeout, + virEventRemoveTimeoutFunc removeTimeout) { + addHandleImpl = addHandle; + removeHandleImpl = removeHandle; + addTimeoutImpl = addTimeout; + removeTimeoutImpl = removeTimeout; +} + + +/* + * Local variables: + * indent-tabs-mode: nil + * c-indent-level: 4 + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ diff --git a/src/event.h b/src/event.h new file mode 100644 index 0000000000..04eb20c5c5 --- /dev/null +++ b/src/event.h @@ -0,0 +1,100 @@ +/* + * event.h: event loop for monitoring file handles + * + * Copyright (C) 2007 Daniel P. Berrange + * Copyright (C) 2007 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Daniel P. Berrange + */ + +#ifndef __VIR_EVENT_H__ +#define __VIR_EVENT_H__ + + +/** + * virEventHandleCallback: callback for receiving file handle events + * + * @fd: file handle on which the event occured + * @events: bitset of events from POLLnnn constants + * @opaque: user data registered with handle + */ +typedef void (*virEventHandleCallback)(int fd, int events, void *opaque); + +/** + * virEventAddHandle: register a callback for monitoring file handle events + * + * @fd: file handle to monitor for events + * @events: bitset of events to wach from POLLnnn constants + * @cb: callback to invoke when an event occurrs + * @opaque: user data to pass to callback + * + * returns -1 if the file handle cannot be registered, 0 upon success + */ +int virEventAddHandle(int fd, int events, virEventHandleCallback cb, void *opaque); + +/** + * virEventRemoveHandle: unregister a callback from a file handle + * + * @fd: file handle to stop monitoring for events + * + * returns -1 if the file handle was not registered, 0 upon success + */ +int virEventRemoveHandle(int fd); + +/** + * virEventTimeoutCallback: callback for receiving timer events + * + * @timer: timer id emitting the event + * @opaque: user data registered with handle + */ +typedef void (*virEventTimeoutCallback)(int timer, void *opaque); + +/** + * virEventAddTimeout: register a callback for a timer event + * + * @timeout: timeout between events in milliseconds + * @cb: callback to invoke when an event occurrs + * @opaque: user data to pass to callback + * + * returns -1 if the file handle cannot be registered, a positive + * integer timer id upon success + */ +int virEventAddTimeout(int timeout, virEventTimeoutCallback cb, void *opaque); + +/** + * virEventRemoveTimeout: unregister a callback for a timer + * + * @timer: the timer id to remove + * + * returns -1 if the timer was not registered, 0 upon success + */ +int virEventRemoveTimeout(int timer); + +typedef int (*virEventAddHandleFunc)(int, int, virEventHandleCallback, void *); +typedef int (*virEventRemoveHandleFunc)(int); + +typedef int (*virEventAddTimeoutFunc)(int, virEventTimeoutCallback, void *); +typedef int (*virEventRemoveTimeoutFunc)(int); + +void __virEventRegisterImpl(virEventAddHandleFunc addHandle, + virEventRemoveHandleFunc removeHandle, + virEventAddTimeoutFunc addTimeout, + virEventRemoveTimeoutFunc removeTimeout); + +#define virEventRegisterImpl(ah,rh,at,rt) __virEventRegisterImpl(ah,rh,at,rt) + +#endif /* __VIR_EVENT_H__ */ diff --git a/src/libvirt_sym.version b/src/libvirt_sym.version index 01441491e7..d71ca8e197 100644 --- a/src/libvirt_sym.version +++ b/src/libvirt_sym.version @@ -91,6 +91,12 @@ virNetworkGetAutostart; virNetworkSetAutostart; + /* Symbols with __ are private only + for use by the libvirtd daemon. + They are not part of stable ABI + guarentee provided for the public + symbols above */ + __virConfNew; __virConfReadFile; __virConfReadMem; @@ -103,5 +109,7 @@ __virGetDomain; __virGetNetwork; + __virEventRegisterImpl; + local: *; }; -- GitLab