2007-06-26 19:11:00 +00:00
|
|
|
/*
|
2008-05-15 06:12:32 +00:00
|
|
|
* event.c: event loop for monitoring file handles
|
2007-06-26 19:11:00 +00:00
|
|
|
*
|
|
|
|
* 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 <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-06-26 19:11:00 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <errno.h>
|
2008-12-04 22:14:15 +00:00
|
|
|
#include <unistd.h>
|
2007-06-26 19:11:00 +00:00
|
|
|
|
Standardize use of header files, making internal.h primary.
* qemud/internal.h, qemud/qemud.h: Rename this file so it
doesn't conflict with src/internal.h.
* HACKING: Document how header files should be used.
* qemud/Makefile.am: Add src/ directory to includes.
* qemud/event.c, qemud/mdns.c, qemud/qemud.c, qemud/remote.c,
qemud/remote_protocol.c, qemud/remote_protocol.h,
qemud/remote_protocol.x, src/buf.c, src/libvirt.c,
src/nodeinfo.c, src/qemu_conf.c, src/qemu_driver.c,
src/stats_linux.c, src/storage_backend.c, src/storage_backend_fs.c,
src/storage_backend_iscsi.c, src/storage_backend_logical.c,
src/storage_conf.c, src/storage_driver.c, src/util.c,
src/util.h, src/virsh.c, src/virterror.c, src/xend_internal.c,
src/xml.c, tests/reconnect.c, tests/xmlrpctest.c,
tests/qparamtest.c: Standardize use of header files.
* docs/*, po/*: Rebuild docs.
2008-05-23 08:24:41 +00:00
|
|
|
#include "qemud.h"
|
2007-06-26 19:11:00 +00:00
|
|
|
#include "event.h"
|
2008-06-06 10:52:01 +00:00
|
|
|
#include "memory.h"
|
2008-12-04 22:14:15 +00:00
|
|
|
#include "util.h"
|
2007-06-26 19:11:00 +00:00
|
|
|
|
2008-12-22 12:55:47 +00:00
|
|
|
#define EVENT_DEBUG(fmt, ...) DEBUG(fmt, __VA_ARGS__)
|
2007-09-19 01:27:32 +00:00
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
static int virEventInterruptLocked(void);
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
/* State for a single file handle being monitored */
|
|
|
|
struct virEventHandle {
|
2008-11-19 16:19:36 +00:00
|
|
|
int watch;
|
2007-06-26 19:11:00 +00:00
|
|
|
int fd;
|
|
|
|
int events;
|
|
|
|
virEventHandleCallback cb;
|
2008-11-19 16:24:01 +00:00
|
|
|
virFreeCallback ff;
|
2007-06-26 19:11:00 +00:00
|
|
|
void *opaque;
|
|
|
|
int deleted;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* State for a single timer being generated */
|
|
|
|
struct virEventTimeout {
|
|
|
|
int timer;
|
2007-09-19 01:27:32 +00:00
|
|
|
int frequency;
|
2007-06-26 19:11:00 +00:00
|
|
|
unsigned long long expiresAt;
|
|
|
|
virEventTimeoutCallback cb;
|
2008-11-19 16:24:01 +00:00
|
|
|
virFreeCallback ff;
|
2007-06-26 19:11:00 +00:00
|
|
|
void *opaque;
|
|
|
|
int deleted;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Allocate extra slots for virEventHandle/virEventTimeout
|
|
|
|
records in this multiple */
|
|
|
|
#define EVENT_ALLOC_EXTENT 10
|
|
|
|
|
|
|
|
/* State for the main event loop */
|
|
|
|
struct virEventLoop {
|
2008-12-04 22:14:15 +00:00
|
|
|
pthread_mutex_t lock;
|
2009-02-06 14:43:52 +00:00
|
|
|
int running;
|
2008-12-04 22:14:15 +00:00
|
|
|
pthread_t leader;
|
|
|
|
int wakeupfd[2];
|
2007-06-26 19:11:00 +00:00
|
|
|
int handlesCount;
|
|
|
|
int handlesAlloc;
|
|
|
|
struct virEventHandle *handles;
|
|
|
|
int timeoutsCount;
|
|
|
|
int timeoutsAlloc;
|
|
|
|
struct virEventTimeout *timeouts;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Only have one event loop */
|
|
|
|
static struct virEventLoop eventLoop;
|
|
|
|
|
2008-11-19 16:19:36 +00:00
|
|
|
/* Unique ID for the next FD watch to be registered */
|
|
|
|
static int nextWatch = 0;
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
/* Unique ID for the next timer to be registered */
|
|
|
|
static int nextTimer = 0;
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
static void virEventLock(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&eventLoop.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virEventUnlock(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&eventLoop.lock);
|
|
|
|
}
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
/*
|
|
|
|
* Register a callback for monitoring file handle events.
|
|
|
|
* NB, it *must* be safe to call this from within a callback
|
|
|
|
* For this reason we only ever append to existing list.
|
|
|
|
*/
|
2008-11-19 16:24:01 +00:00
|
|
|
int virEventAddHandleImpl(int fd, int events,
|
|
|
|
virEventHandleCallback cb,
|
|
|
|
void *opaque,
|
|
|
|
virFreeCallback ff) {
|
2008-12-04 22:14:15 +00:00
|
|
|
int watch;
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Add handle %d %d %p %p", fd, events, cb, opaque);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
if (eventLoop.handlesCount == eventLoop.handlesAlloc) {
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Used %d handle slots, adding %d more",
|
|
|
|
eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
|
2008-06-06 10:52:01 +00:00
|
|
|
if (VIR_REALLOC_N(eventLoop.handles,
|
2008-12-04 22:14:15 +00:00
|
|
|
(eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0) {
|
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
2008-12-04 22:14:15 +00:00
|
|
|
}
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
|
|
|
|
}
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
watch = nextWatch++;
|
|
|
|
|
|
|
|
eventLoop.handles[eventLoop.handlesCount].watch = watch;
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handles[eventLoop.handlesCount].fd = fd;
|
2008-10-23 13:18:18 +00:00
|
|
|
eventLoop.handles[eventLoop.handlesCount].events =
|
|
|
|
virEventHandleTypeToPollEvent(events);
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handles[eventLoop.handlesCount].cb = cb;
|
2008-11-19 16:24:01 +00:00
|
|
|
eventLoop.handles[eventLoop.handlesCount].ff = ff;
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handles[eventLoop.handlesCount].opaque = opaque;
|
|
|
|
eventLoop.handles[eventLoop.handlesCount].deleted = 0;
|
|
|
|
|
|
|
|
eventLoop.handlesCount++;
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
|
|
|
|
|
|
|
return watch;
|
2007-06-26 19:11:00 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 16:19:36 +00:00
|
|
|
void virEventUpdateHandleImpl(int watch, int events) {
|
2007-09-19 01:27:32 +00:00
|
|
|
int i;
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-09-19 01:27:32 +00:00
|
|
|
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
|
2008-11-19 16:19:36 +00:00
|
|
|
if (eventLoop.handles[i].watch == watch) {
|
2008-10-23 13:18:18 +00:00
|
|
|
eventLoop.handles[i].events =
|
|
|
|
virEventHandleTypeToPollEvent(events);
|
2007-09-19 01:27:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
2007-09-19 01:27:32 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
/*
|
|
|
|
* Unregister a callback from a file handle
|
|
|
|
* NB, it *must* be safe to call this from within a callback
|
|
|
|
* For this reason we only ever set a flag in the existing list.
|
|
|
|
* Actual deletion will be done out-of-band
|
|
|
|
*/
|
2008-11-19 16:19:36 +00:00
|
|
|
int virEventRemoveHandleImpl(int watch) {
|
2007-06-26 19:11:00 +00:00
|
|
|
int i;
|
2008-11-19 16:19:36 +00:00
|
|
|
EVENT_DEBUG("Remove handle %d", watch);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
|
|
|
|
if (eventLoop.handles[i].deleted)
|
|
|
|
continue;
|
|
|
|
|
2008-11-19 16:19:36 +00:00
|
|
|
if (eventLoop.handles[i].watch == watch) {
|
|
|
|
EVENT_DEBUG("mark delete %d %d", i, eventLoop.handles[i].fd);
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handles[i].deleted = 1;
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a callback for a timer event
|
|
|
|
* NB, it *must* be safe to call this from within a callback
|
|
|
|
* For this reason we only ever append to existing list.
|
|
|
|
*/
|
2008-11-19 16:24:01 +00:00
|
|
|
int virEventAddTimeoutImpl(int frequency,
|
|
|
|
virEventTimeoutCallback cb,
|
|
|
|
void *opaque,
|
|
|
|
virFreeCallback ff) {
|
2007-09-19 01:27:32 +00:00
|
|
|
struct timeval now;
|
2008-12-04 22:14:15 +00:00
|
|
|
int ret;
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Adding timer %d with %d ms freq", nextTimer, frequency);
|
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) {
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Used %d timeout slots, adding %d more",
|
|
|
|
eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
|
2008-06-06 10:52:01 +00:00
|
|
|
if (VIR_REALLOC_N(eventLoop.timeouts,
|
2008-12-04 22:14:15 +00:00
|
|
|
(eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0) {
|
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
2008-12-04 22:14:15 +00:00
|
|
|
}
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].timer = nextTimer++;
|
2007-09-19 01:27:32 +00:00
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].frequency = frequency;
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].cb = cb;
|
2008-11-19 16:24:01 +00:00
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].ff = ff;
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].opaque = opaque;
|
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].deleted = 0;
|
|
|
|
eventLoop.timeouts[eventLoop.timeoutsCount].expiresAt =
|
2007-09-19 01:27:32 +00:00
|
|
|
frequency >= 0 ? frequency +
|
|
|
|
(((unsigned long long)now.tv_sec)*1000) +
|
|
|
|
(((unsigned long long)now.tv_usec)/1000) : 0;
|
2007-06-26 19:11:00 +00:00
|
|
|
|
|
|
|
eventLoop.timeoutsCount++;
|
2008-12-04 22:14:15 +00:00
|
|
|
ret = nextTimer-1;
|
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
|
|
|
return ret;
|
2007-06-26 19:11:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-19 01:27:32 +00:00
|
|
|
void virEventUpdateTimeoutImpl(int timer, int frequency) {
|
|
|
|
struct timeval tv;
|
|
|
|
int i;
|
|
|
|
EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency);
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-09-19 01:27:32 +00:00
|
|
|
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
|
|
|
|
if (eventLoop.timeouts[i].timer == timer) {
|
|
|
|
eventLoop.timeouts[i].frequency = frequency;
|
|
|
|
eventLoop.timeouts[i].expiresAt =
|
|
|
|
frequency >= 0 ? frequency +
|
|
|
|
(((unsigned long long)tv.tv_sec)*1000) +
|
|
|
|
(((unsigned long long)tv.tv_usec)/1000) : 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
2007-09-19 01:27:32 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
/*
|
|
|
|
* Unregister a callback for a timer
|
|
|
|
* NB, it *must* be safe to call this from within a callback
|
|
|
|
* For this reason we only ever set a flag in the existing list.
|
|
|
|
* Actual deletion will be done out-of-band
|
|
|
|
*/
|
2007-06-26 22:51:01 +00:00
|
|
|
int virEventRemoveTimeoutImpl(int timer) {
|
2007-06-26 19:11:00 +00:00
|
|
|
int i;
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Remove timer %d", timer);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
|
|
|
|
if (eventLoop.timeouts[i].deleted)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (eventLoop.timeouts[i].timer == timer) {
|
|
|
|
eventLoop.timeouts[i].deleted = 1;
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterates over all registered timeouts and determine which
|
|
|
|
* will be the first to expire.
|
|
|
|
* @timeout: filled with expiry time of soonest timer, or -1 if
|
|
|
|
* no timeout is pending
|
|
|
|
* returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
static int virEventCalculateTimeout(int *timeout) {
|
|
|
|
unsigned long long then = 0;
|
|
|
|
int i;
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Calculate expiry of %d timers", eventLoop.timeoutsCount);
|
2007-06-26 19:11:00 +00:00
|
|
|
/* Figure out if we need a timeout */
|
|
|
|
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
|
2007-09-19 01:27:32 +00:00
|
|
|
if (eventLoop.timeouts[i].deleted || eventLoop.timeouts[i].frequency < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
continue;
|
|
|
|
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Got a timeout scheduled for %llu", eventLoop.timeouts[i].expiresAt);
|
2007-06-26 19:11:00 +00:00
|
|
|
if (then == 0 ||
|
|
|
|
eventLoop.timeouts[i].expiresAt < then)
|
|
|
|
then = eventLoop.timeouts[i].expiresAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate how long we should wait for a timeout if needed */
|
|
|
|
if (then > 0) {
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*timeout = then -
|
|
|
|
((((unsigned long long)tv.tv_sec)*1000) +
|
|
|
|
(((unsigned long long)tv.tv_usec)/1000));
|
|
|
|
|
|
|
|
if (*timeout < 0)
|
2007-09-19 01:27:32 +00:00
|
|
|
*timeout = 0;
|
2007-06-26 19:11:00 +00:00
|
|
|
} else {
|
|
|
|
*timeout = -1;
|
|
|
|
}
|
|
|
|
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Timeout at %llu due in %d ms", then, *timeout);
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a pollfd array containing data for all registered
|
|
|
|
* file handles. The caller must free the returned data struct
|
|
|
|
* returns: the pollfd array, or NULL on error
|
|
|
|
*/
|
|
|
|
static int virEventMakePollFDs(struct pollfd **retfds) {
|
|
|
|
struct pollfd *fds;
|
|
|
|
int i, nfds = 0;
|
|
|
|
|
|
|
|
for (i = 0 ; i < eventLoop.handlesCount ; i++) {
|
|
|
|
if (eventLoop.handles[i].deleted)
|
|
|
|
continue;
|
|
|
|
nfds++;
|
|
|
|
}
|
|
|
|
*retfds = NULL;
|
|
|
|
/* Setup the poll file handle data structs */
|
2008-06-06 10:52:01 +00:00
|
|
|
if (VIR_ALLOC_N(fds, nfds) < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0, nfds = 0 ; i < eventLoop.handlesCount ; i++) {
|
|
|
|
if (eventLoop.handles[i].deleted)
|
|
|
|
continue;
|
|
|
|
fds[nfds].fd = eventLoop.handles[i].fd;
|
|
|
|
fds[nfds].events = eventLoop.handles[i].events;
|
|
|
|
fds[nfds].revents = 0;
|
2007-09-19 01:27:32 +00:00
|
|
|
//EVENT_DEBUG("Wait for %d %d", eventLoop.handles[i].fd, eventLoop.handles[i].events);
|
2007-06-26 19:11:00 +00:00
|
|
|
nfds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*retfds = fds;
|
|
|
|
return nfds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over all timers and determine if any have expired.
|
|
|
|
* Invoke the user supplied callback for each timer whose
|
|
|
|
* expiry time is met, and schedule the next timeout. Does
|
|
|
|
* not try to 'catch up' on time if the actual expiry time
|
|
|
|
* was later than the requested time.
|
|
|
|
*
|
|
|
|
* This method must cope with new timers being registered
|
|
|
|
* by a callback, and must skip any timers marked as deleted.
|
|
|
|
*
|
|
|
|
* Returns 0 upon success, -1 if an error occurred
|
|
|
|
*/
|
|
|
|
static int virEventDispatchTimeouts(void) {
|
|
|
|
struct timeval tv;
|
|
|
|
unsigned long long now;
|
|
|
|
int i;
|
|
|
|
/* Save this now - it may be changed during dispatch */
|
|
|
|
int ntimeouts = eventLoop.timeoutsCount;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
now = (((unsigned long long)tv.tv_sec)*1000) +
|
|
|
|
(((unsigned long long)tv.tv_usec)/1000);
|
|
|
|
|
|
|
|
for (i = 0 ; i < ntimeouts ; i++) {
|
2007-09-19 01:27:32 +00:00
|
|
|
if (eventLoop.timeouts[i].deleted || eventLoop.timeouts[i].frequency < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (eventLoop.timeouts[i].expiresAt <= now) {
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventTimeoutCallback cb = eventLoop.timeouts[i].cb;
|
|
|
|
int timer = eventLoop.timeouts[i].timer;
|
|
|
|
void *opaque = eventLoop.timeouts[i].opaque;
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.timeouts[i].expiresAt =
|
2007-09-19 01:27:32 +00:00
|
|
|
now + eventLoop.timeouts[i].frequency;
|
2008-12-04 22:14:15 +00:00
|
|
|
|
|
|
|
virEventUnlock();
|
|
|
|
(cb)(timer, opaque);
|
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Iterate over all file handles and dispatch any which
|
|
|
|
* have pending events listed in the poll() data. Invoke
|
|
|
|
* the user supplied callback for each handle which has
|
|
|
|
* pending events
|
|
|
|
*
|
|
|
|
* This method must cope with new handles being registered
|
|
|
|
* by a callback, and must skip any handles marked as deleted.
|
|
|
|
*
|
|
|
|
* Returns 0 upon success, -1 if an error occurred
|
|
|
|
*/
|
2008-12-04 22:14:15 +00:00
|
|
|
static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
|
2009-03-16 10:35:21 +00:00
|
|
|
int i, n;
|
2007-06-26 19:11:00 +00:00
|
|
|
|
2009-03-16 10:35:21 +00:00
|
|
|
for (i = 0, n = 0 ; i < eventLoop.handlesCount && n < nfds ; i++) {
|
2007-06-26 19:11:00 +00:00
|
|
|
if (eventLoop.handles[i].deleted) {
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Skip deleted %d", eventLoop.handles[i].fd);
|
2007-06-26 19:11:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-16 10:35:21 +00:00
|
|
|
if (fds[n].revents) {
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventHandleCallback cb = eventLoop.handles[i].cb;
|
|
|
|
void *opaque = eventLoop.handles[i].opaque;
|
2009-03-16 10:35:21 +00:00
|
|
|
int hEvents = virPollEventToEventHandleType(fds[n].revents);
|
|
|
|
EVENT_DEBUG("Dispatch %d %d %p", fds[n].fd,
|
|
|
|
fds[n].revents, eventLoop.handles[i].opaque);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
|
|
|
(cb)(eventLoop.handles[i].watch,
|
2009-03-16 10:35:21 +00:00
|
|
|
fds[n].fd, hEvents, opaque);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
}
|
2009-03-16 10:35:21 +00:00
|
|
|
n++;
|
2007-06-26 19:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Used post dispatch to actually remove any timers that
|
|
|
|
* were previously marked as deleted. This asynchronous
|
|
|
|
* cleanup is needed to make dispatch re-entrant safe.
|
|
|
|
*/
|
|
|
|
static int virEventCleanupTimeouts(void) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Remove deleted entries, shuffling down remaining
|
2008-02-29 12:53:10 +00:00
|
|
|
* entries as needed to form contiguous series
|
2007-06-26 19:11:00 +00:00
|
|
|
*/
|
|
|
|
for (i = 0 ; i < eventLoop.timeoutsCount ; ) {
|
|
|
|
if (!eventLoop.timeouts[i].deleted) {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Purging timeout %d with id %d", i, eventLoop.timeouts[i].timer);
|
2008-11-19 16:24:01 +00:00
|
|
|
if (eventLoop.timeouts[i].ff)
|
|
|
|
(eventLoop.timeouts[i].ff)(eventLoop.timeouts[i].opaque);
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
if ((i+1) < eventLoop.timeoutsCount) {
|
|
|
|
memmove(eventLoop.timeouts+i,
|
|
|
|
eventLoop.timeouts+i+1,
|
|
|
|
sizeof(struct virEventTimeout)*(eventLoop.timeoutsCount-(i+1)));
|
|
|
|
}
|
|
|
|
eventLoop.timeoutsCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release some memory if we've got a big chunk free */
|
|
|
|
if ((eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT) > eventLoop.timeoutsCount) {
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Releasing %d out of %d timeout slots used, releasing %d",
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.timeoutsCount, eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
|
2008-06-06 10:52:01 +00:00
|
|
|
if (VIR_REALLOC_N(eventLoop.timeouts,
|
|
|
|
(eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT)) < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
eventLoop.timeoutsAlloc -= EVENT_ALLOC_EXTENT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used post dispatch to actually remove any handles that
|
|
|
|
* were previously marked as deleted. This asynchronous
|
|
|
|
* cleanup is needed to make dispatch re-entrant safe.
|
|
|
|
*/
|
|
|
|
static int virEventCleanupHandles(void) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Remove deleted entries, shuffling down remaining
|
2008-02-29 12:53:10 +00:00
|
|
|
* entries as needed to form contiguous series
|
2007-06-26 19:11:00 +00:00
|
|
|
*/
|
|
|
|
for (i = 0 ; i < eventLoop.handlesCount ; ) {
|
|
|
|
if (!eventLoop.handles[i].deleted) {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-19 16:24:01 +00:00
|
|
|
if (eventLoop.handles[i].ff)
|
|
|
|
(eventLoop.handles[i].ff)(eventLoop.handles[i].opaque);
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
if ((i+1) < eventLoop.handlesCount) {
|
|
|
|
memmove(eventLoop.handles+i,
|
|
|
|
eventLoop.handles+i+1,
|
|
|
|
sizeof(struct virEventHandle)*(eventLoop.handlesCount-(i+1)));
|
|
|
|
}
|
|
|
|
eventLoop.handlesCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release some memory if we've got a big chunk free */
|
|
|
|
if ((eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT) > eventLoop.handlesCount) {
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Releasing %d out of %d handles slots used, releasing %d",
|
2007-06-26 19:11:00 +00:00
|
|
|
eventLoop.handlesCount, eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
|
2008-06-06 10:52:01 +00:00
|
|
|
if (VIR_REALLOC_N(eventLoop.handles,
|
|
|
|
(eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT)) < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
eventLoop.handlesAlloc -= EVENT_ALLOC_EXTENT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run a single iteration of the event loop, blocking until
|
|
|
|
* at least one file handle has an event, or a timer expires
|
|
|
|
*/
|
|
|
|
int virEventRunOnce(void) {
|
|
|
|
struct pollfd *fds;
|
|
|
|
int ret, timeout, nfds;
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventLock();
|
2009-02-06 14:43:52 +00:00
|
|
|
eventLoop.running = 1;
|
2008-12-04 22:14:15 +00:00
|
|
|
eventLoop.leader = pthread_self();
|
|
|
|
if ((nfds = virEventMakePollFDs(&fds)) < 0) {
|
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
2008-12-04 22:14:15 +00:00
|
|
|
}
|
2007-06-26 19:11:00 +00:00
|
|
|
|
|
|
|
if (virEventCalculateTimeout(&timeout) < 0) {
|
2008-06-06 10:52:01 +00:00
|
|
|
VIR_FREE(fds);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
|
|
|
|
2007-06-26 19:11:00 +00:00
|
|
|
retry:
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Poll on %d handles %p timeout %d", nfds, fds, timeout);
|
2007-06-26 19:11:00 +00:00
|
|
|
ret = poll(fds, nfds, timeout);
|
2007-09-19 01:27:32 +00:00
|
|
|
EVENT_DEBUG("Poll got %d event", ret);
|
2007-06-26 19:11:00 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
goto retry;
|
|
|
|
}
|
2008-06-06 10:52:01 +00:00
|
|
|
VIR_FREE(fds);
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-04 22:14:15 +00:00
|
|
|
|
|
|
|
virEventLock();
|
2007-06-26 19:11:00 +00:00
|
|
|
if (virEventDispatchTimeouts() < 0) {
|
2008-06-06 10:52:01 +00:00
|
|
|
VIR_FREE(fds);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > 0 &&
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventDispatchHandles(nfds, fds) < 0) {
|
2008-06-06 10:52:01 +00:00
|
|
|
VIR_FREE(fds);
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-06-06 10:52:01 +00:00
|
|
|
VIR_FREE(fds);
|
2007-06-26 19:11:00 +00:00
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
if (virEventCleanupTimeouts() < 0) {
|
|
|
|
virEventUnlock();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virEventCleanupHandles() < 0) {
|
|
|
|
virEventUnlock();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-06 14:43:52 +00:00
|
|
|
eventLoop.running = 0;
|
2008-12-04 22:14:15 +00:00
|
|
|
virEventUnlock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virEventHandleWakeup(int watch ATTRIBUTE_UNUSED,
|
|
|
|
int fd,
|
|
|
|
int events ATTRIBUTE_UNUSED,
|
|
|
|
void *opaque ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
virEventLock();
|
|
|
|
saferead(fd, &c, sizeof(c));
|
|
|
|
virEventUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
int virEventInit(void)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_init(&eventLoop.lock, NULL) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pipe(eventLoop.wakeupfd) < 0 ||
|
|
|
|
qemudSetNonBlock(eventLoop.wakeupfd[0]) < 0 ||
|
|
|
|
qemudSetNonBlock(eventLoop.wakeupfd[1]) < 0 ||
|
|
|
|
qemudSetCloseExec(eventLoop.wakeupfd[0]) < 0 ||
|
|
|
|
qemudSetCloseExec(eventLoop.wakeupfd[1]) < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
if (virEventAddHandleImpl(eventLoop.wakeupfd[0],
|
|
|
|
VIR_EVENT_HANDLE_READABLE,
|
|
|
|
virEventHandleWakeup, NULL, NULL) < 0)
|
2007-06-26 19:11:00 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-23 13:18:18 +00:00
|
|
|
|
2008-12-04 22:14:15 +00:00
|
|
|
static int virEventInterruptLocked(void)
|
|
|
|
{
|
|
|
|
char c = '\0';
|
2009-02-06 14:43:52 +00:00
|
|
|
|
|
|
|
if (!eventLoop.running ||
|
|
|
|
pthread_self() == eventLoop.leader)
|
2008-12-04 22:14:15 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (safewrite(eventLoop.wakeupfd[1], &c, sizeof(c)) != sizeof(c))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virEventInterrupt(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
virEventLock();
|
|
|
|
ret = virEventInterruptLocked();
|
|
|
|
virEventUnlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-23 13:18:18 +00:00
|
|
|
int
|
2008-11-04 23:33:57 +00:00
|
|
|
virEventHandleTypeToPollEvent(int events)
|
2008-10-23 13:18:18 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if(events & VIR_EVENT_HANDLE_READABLE)
|
|
|
|
ret |= POLLIN;
|
|
|
|
if(events & VIR_EVENT_HANDLE_WRITABLE)
|
|
|
|
ret |= POLLOUT;
|
|
|
|
if(events & VIR_EVENT_HANDLE_ERROR)
|
|
|
|
ret |= POLLERR;
|
|
|
|
if(events & VIR_EVENT_HANDLE_HANGUP)
|
|
|
|
ret |= POLLHUP;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-04 23:33:57 +00:00
|
|
|
int
|
|
|
|
virPollEventToEventHandleType(int events)
|
2008-10-23 13:18:18 +00:00
|
|
|
{
|
2008-11-04 23:33:57 +00:00
|
|
|
int ret = 0;
|
2008-10-23 13:18:18 +00:00
|
|
|
if(events & POLLIN)
|
|
|
|
ret |= VIR_EVENT_HANDLE_READABLE;
|
|
|
|
if(events & POLLOUT)
|
|
|
|
ret |= VIR_EVENT_HANDLE_WRITABLE;
|
|
|
|
if(events & POLLERR)
|
|
|
|
ret |= VIR_EVENT_HANDLE_ERROR;
|
2009-02-17 09:44:18 +00:00
|
|
|
if(events & POLLNVAL) /* Treat NVAL as error, since libvirt doesn't distinguish */
|
|
|
|
ret |= VIR_EVENT_HANDLE_ERROR;
|
2008-10-23 13:18:18 +00:00
|
|
|
if(events & POLLHUP)
|
|
|
|
ret |= VIR_EVENT_HANDLE_HANGUP;
|
|
|
|
return ret;
|
|
|
|
}
|