summaryrefslogtreecommitdiff
path: root/lib/libevent
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2005-04-19 08:07:46 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2005-04-19 08:07:46 +0000
commit1515dac3f44395b86eb4a2bd730cf6e165823673 (patch)
treee3c3f7e99b7a7689b107f148c05fe7e8c4626b7b /lib/libevent
parent755a6977af2b359f9b5481ad6019880b4afef19d (diff)
backout. not discussed, and very wrong. bad brad
Diffstat (limited to 'lib/libevent')
-rw-r--r--lib/libevent/Makefile22
-rw-r--r--lib/libevent/buffer.c202
-rw-r--r--lib/libevent/err.c97
-rw-r--r--lib/libevent/evbuffer.c18
-rw-r--r--lib/libevent/event-internal.h56
-rw-r--r--lib/libevent/event.379
-rw-r--r--lib/libevent/event.c408
-rw-r--r--lib/libevent/event.h59
-rw-r--r--lib/libevent/kqueue.c63
-rw-r--r--lib/libevent/log.c208
-rw-r--r--lib/libevent/log.h45
-rw-r--r--lib/libevent/poll.c46
-rw-r--r--lib/libevent/select.c50
-rw-r--r--lib/libevent/signal.c74
14 files changed, 365 insertions, 1062 deletions
diff --git a/lib/libevent/Makefile b/lib/libevent/Makefile
index a27acb85724..c4bd89e6364 100644
--- a/lib/libevent/Makefile
+++ b/lib/libevent/Makefile
@@ -1,15 +1,13 @@
-# $OpenBSD: Makefile,v 1.10 2005/04/19 02:03:12 brad Exp $
+# $OpenBSD: Makefile,v 1.11 2005/04/19 08:07:45 deraadt Exp $
LIB= event
-SRCS= buffer.c evbuffer.c event.c kqueue.c log.c poll.c select.c signal.c
+SRCS= buffer.c evbuffer.c event.c kqueue.c poll.c select.c signal.c
HDRS= event.h
MAN= event.3
MLINKS= event.3 event_init.3 event.3 event_dispatch.3 event.3 event_loop.3 \
- event.3 event_loopexit.3 event.3 event_base_loop.3 event.3 \
- event_base_loopexit.3 event.3 event_set.3 event.3 event_add.3 event.3 \
+ event.3 event_loopexit.3 event.3 event_set.3 event.3 event_add.3 event.3 \
event_del.3 event.3 event_once.3 event.3 event_pending.3 event.3 \
- event_initialized.3 event.3 event_priority_init.3 event.3 \
- ievent_priority_set.3 event.3 evtimer_set.3 event.3 evtimer_add.3 event.3 \
+ event_initialized.3 event.3 evtimer_set.3 event.3 evtimer_add.3 event.3 \
evtimer_del.3 event.3 evtimer_pending.3 event.3 evtimer_initialized.3 \
event.3 signal_set.3 event.3 signal_add.3 event.3 signal_del.3 \
event.3 signal_pending.3 event.3 signal_initialized.3 event.3 \
@@ -22,16 +20,12 @@ MLINKS= event.3 event_init.3 event.3 event_dispatch.3 event.3 event_loop.3 \
event.3 evbuffer_read.3 event.3 evbuffer_find.3
CFLAGS+= -I${.CURDIR} \
- -DHAVE_FCNTL_H \
- -DHAVE_POLL \
+ -DHAVE_WORKING_KQUEUE \
-DHAVE_SELECT \
- -DHAVE_SETFD \
- -DHAVE_STDARG_H \
- -DHAVE_SYS_IOCTL_H \
+ -DHAVE_POLL \
-DHAVE_SYS_TIME_H \
- -DHAVE_UNISTD_H \
- -DHAVE_VASPRINTF \
- -DHAVE_WORKING_KQUEUE
+ -DHAVE_STDARG_H \
+ -DHAVE_UNISTD_H
NOPIC=
diff --git a/lib/libevent/buffer.c b/lib/libevent/buffer.c
index 9645940c514..7755e7f1b53 100644
--- a/lib/libevent/buffer.c
+++ b/lib/libevent/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.2 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: buffer.c,v 1.3 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
@@ -27,25 +27,17 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <sys/types.h>
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
-#ifdef HAVE_VASPRINTF
-/* If we have vasprintf, we need to define this before we include stdio.h. */
-#define _GNU_SOURCE
-#endif
-
-#include <sys/types.h>
-
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef HAVE_SYS_IOCTL_H
-#include <sys/ioctl.h>
-#endif
-
+#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -72,8 +64,8 @@ evbuffer_new(void)
void
evbuffer_free(struct evbuffer *buffer)
{
- if (buffer->orig_buffer != NULL)
- free(buffer->orig_buffer);
+ if (buffer->buffer != NULL)
+ free(buffer->buffer);
free(buffer);
}
@@ -82,42 +74,10 @@ evbuffer_free(struct evbuffer *buffer)
* the other buffer.
*/
-#define SWAP(x,y) do { \
- (x)->buffer = (y)->buffer; \
- (x)->orig_buffer = (y)->orig_buffer; \
- (x)->misalign = (y)->misalign; \
- (x)->totallen = (y)->totallen; \
- (x)->off = (y)->off; \
-} while (0)
-
int
evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
{
int res;
-
- /* Short cut for better performance */
- if (outbuf->off == 0) {
- struct evbuffer tmp;
- size_t oldoff = inbuf->off;
-
- /* Swap them directly */
- SWAP(&tmp, outbuf);
- SWAP(outbuf, inbuf);
- SWAP(inbuf, &tmp);
-
- /*
- * Optimization comes with a price; we need to notify the
- * buffer if necessary of the changes. oldoff is the amount
- * of data that we tranfered from inbuf to outbuf
- */
- if (inbuf->off != oldoff && inbuf->cb != NULL)
- (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg);
- if (oldoff && outbuf->cb != NULL)
- (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
-
- return (0);
- }
-
res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
if (res == 0)
evbuffer_drain(inbuf, inbuf->off);
@@ -130,113 +90,46 @@ evbuffer_add_printf(struct evbuffer *buf, char *fmt, ...)
{
int res = -1;
char *msg;
-#ifndef HAVE_VASPRINTF
- static char buffer[4096];
-#endif
va_list ap;
va_start(ap, fmt);
-#ifdef HAVE_VASPRINTF
if (vasprintf(&msg, fmt, ap) == -1)
goto end;
-#else
-# ifdef WIN32
- _vsnprintf(buffer, sizeof(buffer) - 1, fmt, ap);
- buffer[sizeof(buffer)-1] = '\0';
-# else /* ! WIN32 */
- vsnprintf(buffer, sizeof(buffer), fmt, ap);
-# endif
- msg = buffer;
-#endif
res = strlen(msg);
if (evbuffer_add(buf, msg, res) == -1)
res = -1;
-#ifdef HAVE_VASPRINTF
free(msg);
-end:
-#endif
+ end:
va_end(ap);
return (res);
}
-/* Reads data from an event buffer and drains the bytes read */
-
-int
-evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen)
-{
- int nread = datlen;
- if (nread >= buf->off)
- nread = buf->off;
-
- memcpy(data, buf->buffer, nread);
- evbuffer_drain(buf, nread);
-
- return (nread);
-}
-
-/* Adds data to an event buffer */
-
-static inline void
-evbuffer_align(struct evbuffer *buf)
-{
- memmove(buf->orig_buffer, buf->buffer, buf->off);
- buf->buffer = buf->orig_buffer;
- buf->misalign = 0;
-}
-
-/* Expands the available space in the event buffer to at least datlen */
-
int
-evbuffer_expand(struct evbuffer *buf, size_t datlen)
+evbuffer_add(struct evbuffer *buf, u_char *data, size_t datlen)
{
- size_t need = buf->misalign + buf->off + datlen;
-
- /* If we can fit all the data, then we don't have to do anything */
- if (buf->totallen >= need)
- return (0);
+ size_t need = buf->off + datlen;
+ size_t oldoff = buf->off;
- /*
- * If the misalignment fulfills our data needs, we just force an
- * alignment to happen. Afterwards, we have enough space.
- */
- if (buf->misalign >= datlen) {
- evbuffer_align(buf);
- } else {
+ if (buf->totallen < need) {
void *newbuf;
- size_t length = buf->totallen;
+ int length = buf->totallen;
if (length < 256)
length = 256;
while (length < need)
length <<= 1;
- if (buf->orig_buffer != buf->buffer)
- evbuffer_align(buf);
if ((newbuf = realloc(buf->buffer, length)) == NULL)
return (-1);
- buf->orig_buffer = buf->buffer = newbuf;
+ buf->buffer = newbuf;
buf->totallen = length;
}
- return (0);
-}
-
-int
-evbuffer_add(struct evbuffer *buf, void *data, size_t datlen)
-{
- size_t need = buf->misalign + buf->off + datlen;
- size_t oldoff = buf->off;
-
- if (buf->totallen < need) {
- if (evbuffer_expand(buf, datlen) == -1)
- return (-1);
- }
-
memcpy(buf->buffer + buf->off, data, datlen);
buf->off += datlen;
@@ -253,14 +146,10 @@ evbuffer_drain(struct evbuffer *buf, size_t len)
if (len >= buf->off) {
buf->off = 0;
- buf->buffer = buf->orig_buffer;
- buf->misalign = 0;
goto done;
}
- buf->buffer += len;
- buf->misalign += len;
-
+ memmove(buf->buffer, buf->buffer + len, buf->off - len);
buf->off -= len;
done:
@@ -270,56 +159,22 @@ evbuffer_drain(struct evbuffer *buf, size_t len)
}
-/*
- * Reads data from a file descriptor into a buffer.
- */
-
-#define EVBUFFER_MAX_READ 4096
-
int
-evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
+evbuffer_read(struct evbuffer *buffer, int fd, int howmuch)
{
- u_char *p;
- size_t oldoff = buf->off;
- int n = EVBUFFER_MAX_READ;
-#ifdef WIN32
- DWORD dwBytesRead;
-#endif
-
-#ifdef FIONREAD
- if (ioctl(fd, FIONREAD, &n) == -1)
- n = EVBUFFER_MAX_READ;
-#endif
- if (howmuch < 0 || howmuch > n)
- howmuch = n;
-
- /* If we don't have FIONREAD, we might waste some space here */
- if (evbuffer_expand(buf, howmuch) == -1)
- return (-1);
-
- /* We can append new data at this point */
- p = buf->buffer + buf->off;
+ u_char inbuf[4096];
+ int n;
+
+ if (howmuch < 0 || howmuch > sizeof(inbuf))
+ howmuch = sizeof(inbuf);
-#ifndef WIN32
- n = read(fd, p, howmuch);
+ n = read(fd, inbuf, howmuch);
if (n == -1)
return (-1);
if (n == 0)
return (0);
-#else
- n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL);
- if (n == 0)
- return (-1);
- if (dwBytesRead == 0)
- return (0);
- n = dwBytesRead;
-#endif
- buf->off += n;
-
- /* Tell someone about changes in this buffer */
- if (buf->off != oldoff && buf->cb != NULL)
- (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
+ evbuffer_add(buffer, inbuf, n);
return (n);
}
@@ -328,24 +183,13 @@ int
evbuffer_write(struct evbuffer *buffer, int fd)
{
int n;
-#ifdef WIN32
- DWORD dwBytesWritten;
-#endif
-#ifndef WIN32
n = write(fd, buffer->buffer, buffer->off);
if (n == -1)
return (-1);
if (n == 0)
return (0);
-#else
- n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL);
- if (n == 0)
- return (-1);
- if (dwBytesWritten == 0)
- return (0);
- n = dwBytesWritten;
-#endif
+
evbuffer_drain(buffer, n);
return (n);
diff --git a/lib/libevent/err.c b/lib/libevent/err.c
new file mode 100644
index 00000000000..4c9862b80a9
--- /dev/null
+++ b/lib/libevent/err.c
@@ -0,0 +1,97 @@
+/* $OpenBSD: err.c,v 1.5 2005/04/19 08:07:45 deraadt Exp $ */
+
+/*
+ * err.c
+ *
+ * Adapted from OpenBSD libc *err* *warn* code.
+ *
+ * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
+ *
+ * Copyright (c) 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+
+void
+err(int eval, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (fmt != NULL) {
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, ": ");
+ }
+ va_end(ap);
+ (void)fprintf(stderr, "%s\n", strerror(errno));
+ exit(eval);
+}
+
+void
+warn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (fmt != NULL) {
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, ": ");
+ }
+ va_end(ap);
+ (void)fprintf(stderr, "%s\n", strerror(errno));
+}
+
+void
+errx(int eval, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (fmt != NULL)
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, "\n");
+ va_end(ap);
+ exit(eval);
+}
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (fmt != NULL)
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, "\n");
+ va_end(ap);
+}
+
diff --git a/lib/libevent/evbuffer.c b/lib/libevent/evbuffer.c
index 4a3354d9f2c..e0cf27202f1 100644
--- a/lib/libevent/evbuffer.c
+++ b/lib/libevent/evbuffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: evbuffer.c,v 1.2 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: evbuffer.c,v 1.3 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright (c) 2002-2004 Niels Provos <provos@citi.umich.edu>
@@ -37,6 +37,7 @@
#include <sys/time.h>
#endif
+#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -151,9 +152,7 @@ bufferevent_writecb(int fd, short event, void *arg)
if (EVBUFFER_LENGTH(bufev->output)) {
res = evbuffer_write(bufev->output, fd);
if (res == -1) {
- if (errno == EAGAIN ||
- errno == EINTR ||
- errno == EINPROGRESS)
+ if (errno == EAGAIN || errno == EINTR)
goto reschedule;
/* error case */
what |= EVBUFFER_ERROR;
@@ -228,17 +227,6 @@ bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb,
return (bufev);
}
-int
-bufferevent_priority_set(struct bufferevent *bufev, int priority)
-{
- if (event_priority_set(&bufev->ev_read, priority) == -1)
- return (-1);
- if (event_priority_set(&bufev->ev_write, priority) == -1)
- return (-1);
-
- return (0);
-}
-
void
bufferevent_free(struct bufferevent *bufev)
{
diff --git a/lib/libevent/event-internal.h b/lib/libevent/event-internal.h
deleted file mode 100644
index becb6691d52..00000000000
--- a/lib/libevent/event-internal.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-#ifndef _EVENT_INTERNAL_H_
-#define _EVENT_INTERNAL_H_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-struct event_base {
- const struct eventop *evsel;
- void *evbase;
- int event_count; /* counts number of total events */
- int event_count_active; /* counts number of active events */
-
- int event_gotterm; /* Set to terminate loop */
-
- /* active event management */
- struct event_list **activequeues;
- int nactivequeues;
-
- struct event_list eventqueue;
- struct timeval event_tv;
-
- RB_HEAD(event_tree, event) timetree;
-};
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _EVENT_INTERNAL_H_ */
diff --git a/lib/libevent/event.3 b/lib/libevent/event.3
index f8ca1cf6e3e..4d5a52fb90d 100644
--- a/lib/libevent/event.3
+++ b/lib/libevent/event.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: event.3,v 1.15 2005/04/19 02:03:12 brad Exp $
+.\" $OpenBSD: event.3,v 1.16 2005/04/19 08:07:45 deraadt Exp $
.\"
.\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
.\" All rights reserved.
@@ -31,16 +31,12 @@
.Nm event_dispatch ,
.Nm event_loop ,
.Nm event_loopexit ,
-.Nm event_base_loop ,
-.Nm event_base_loopexit ,
.Nm event_set ,
.Nm event_add ,
.Nm event_del ,
.Nm event_once ,
.Nm event_pending ,
.Nm event_initialized ,
-.Nm event_priority_init ,
-.Nm event_priority_set ,
.Nm evtimer_set ,
.Nm evtimer_add ,
.Nm evtimer_del ,
@@ -72,7 +68,7 @@
.Sh SYNOPSIS
.Fd #include <sys/time.h>
.Fd #include <event.h>
-.Ft "struct event_base *"
+.Ft void
.Fn "event_init"
.Ft int
.Fn "event_dispatch"
@@ -80,10 +76,6 @@
.Fn "event_loop" "int flags"
.Ft int
.Fn "event_loopexit" "struct timeval *tv"
-.Ft int
-.Fn "event_base_loop" "struct event_base *" "int flags"
-.Ft int
-.Fn "event_base_loopexit" "struct event_base *" "struct timeval *tv"
.Ft void
.Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
.Ft int
@@ -96,10 +88,6 @@
.Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
.Ft int
.Fn "event_initialized" "struct event *ev"
-.Ft int
-.Fn "event_priority_init" "int npriorities"
-.Ft int
-.Fn "event_priority_set" "struct event *ev" "int priority"
.Ft void
.Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
.Ft void
@@ -274,20 +262,6 @@ and
.Fn event_del
and does not need to be reinitialized unless the function called and/or
the argument to it are to be changed.
-However, when an
-.Fa ev
-structure has been added to libevent using
-.Fn event_add
-the structure must persist until the event occurs (assuming
-.Fa EV_PERSIST
-is not set) or is removed
-using
-.Fn event_del .
-You may not reuse the same
-.Fa ev
-structure for multiple monitored descriptors; each descriptor
-needs its own
-.Fa ev .
.Pp
The function
.Fn event_add
@@ -382,62 +356,17 @@ adds
.Va EV_PERSIST .
.Pp
It is possible to disable support for
-.Va kqueue , poll ,
+.Va epoll , kqueue , poll ,
or
.Va select
by setting the environment variable
-.Va EVENT_NOKQUEUE , EVENT_NOPOLL ,
+.Va EVENT_NOEPOLL , EVENT_NOKQUEUE , EVENT_NOPOLL ,
or
.Va EVENT_NOSELECT .
By setting the environment variable
.Va EVENT_SHOW_METHOD ,
.Nm libevent
displays the kernel notification method that it uses.
-.Pp
-.Sh EVENT PRIORITIES
-By default
-.Nm libevent
-schedules all active events with the same priority.
-However, sometime it is desirable to process some events with a higher
-priority than others.
-For that reason,
-.Nm libevent
-supports strict priority queues.
-Active events with a lower priority are always processed before events
-with a higher priority.
-.Pp
-The number of different priorities can be set initially with the
-.Fn event_priority_init
-function.
-This function should be called before the first call to
-.Fn event_dispatch .
-The
-.Fn event_priority_set
-function can be used to assign a priority to an event.
-By default,
-.Nm libevent
-assigns the middle priority to all events unless their priority
-is explicitly set.
-.Pp
-.Sh THREAD SAFE EVENTS
-.Nm Libevent
-has experimental support for thread-safe events.
-When initializing the library via
-.Fn event_init ,
-an event base is returned.
-This event base can be used in conjunction with calls to
-.Fn event_base_set ,
-.Fn event_base_dispatch ,
-.Fn event_base_loop ,
-and
-.Fn event_base_loopexit .
-.Fn event_base_set
-should be called after preparing an event with
-.Fn event_set ,
-as
-.Fn event_set
-assigns the provided event to the most recently created event base.
-.Pp
.Sh BUFFERED EVENTS
.Nm libevent
provides an abstraction on top of the regular event callbacks.
diff --git a/lib/libevent/event.c b/lib/libevent/event.c
index c4da83803e3..42001fd34fc 100644
--- a/lib/libevent/event.c
+++ b/lib/libevent/event.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: event.c,v 1.7 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: event.c,v 1.8 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
@@ -52,11 +52,17 @@
#include <errno.h>
#include <signal.h>
#include <string.h>
+#include <err.h>
#include <assert.h>
-#include "event.h"
-#include "event-internal.h"
+#ifdef USE_LOG
#include "log.h"
+#else
+#define LOG_DBG(x)
+#define log_error(x) perror(x)
+#endif
+
+#include "event.h"
#ifdef HAVE_SELECT
extern const struct eventop selectops;
@@ -73,9 +79,6 @@ extern const struct eventop epollops;
#ifdef HAVE_WORKING_KQUEUE
extern const struct eventop kqops;
#endif
-#ifdef HAVE_DEVPOLL
-extern const struct eventop devpollops;
-#endif
#ifdef WIN32
extern const struct eventop win32ops;
#endif
@@ -88,9 +91,6 @@ const struct eventop *eventops[] = {
#ifdef HAVE_EPOLL
&epollops,
#endif
-#ifdef HAVE_DEVPOLL
- &devpollops,
-#endif
#ifdef HAVE_RTSIG
&rtsigops,
#endif
@@ -106,25 +106,26 @@ const struct eventop *eventops[] = {
NULL
};
-/* Global state */
-struct event_list signalqueue;
-
-struct event_base *current_base = NULL;
+const struct eventop *evsel;
+void *evbase;
/* Handle signals - This is a deprecated interface */
int (*event_sigcb)(void); /* Signal callback when gotsig is set */
volatile sig_atomic_t event_gotsig; /* Set in signal handler */
+volatile sig_atomic_t event_gotterm; /* Set to terminate loop */
/* Prototypes */
-static void event_queue_insert(struct event_base *, struct event *, int);
-static void event_queue_remove(struct event_base *, struct event *, int);
-static int event_haveevents(struct event_base *);
+void event_queue_insert(struct event *, int);
+void event_queue_remove(struct event *, int);
+int event_haveevents(void);
-static void event_process_active(struct event_base *);
+static void event_process_active(void);
-static int timeout_next(struct event_base *, struct timeval *);
-static void timeout_process(struct event_base *);
-static void timeout_correct(struct event_base *, struct timeval *);
+static RB_HEAD(event_tree, event) timetree;
+static struct event_list activequeue;
+struct event_list signalqueue;
+struct event_list eventqueue;
+static struct timeval event_tv;
static int
compare(struct event *a, struct event *b)
@@ -135,7 +136,7 @@ compare(struct event *a, struct event *b)
return (1);
if (a < b)
return (-1);
- else if (a > b)
+ if (a > b)
return (1);
return (0);
}
@@ -145,112 +146,55 @@ RB_PROTOTYPE(event_tree, event, ev_timeout_node, compare);
RB_GENERATE(event_tree, event, ev_timeout_node, compare);
-void *
+void
event_init(void)
{
int i;
- if ((current_base = calloc(1, sizeof(struct event_base))) == NULL)
- event_err(1, "%s: calloc");
-
event_sigcb = NULL;
event_gotsig = 0;
- gettimeofday(&current_base->event_tv, NULL);
+ gettimeofday(&event_tv, NULL);
- RB_INIT(&current_base->timetree);
- TAILQ_INIT(&current_base->eventqueue);
+ RB_INIT(&timetree);
+ TAILQ_INIT(&eventqueue);
+ TAILQ_INIT(&activequeue);
TAILQ_INIT(&signalqueue);
- current_base->evbase = NULL;
- for (i = 0; eventops[i] && !current_base->evbase; i++) {
- current_base->evsel = eventops[i];
+ evbase = NULL;
+ for (i = 0; eventops[i] && !evbase; i++) {
+ evsel = eventops[i];
- current_base->evbase = current_base->evsel->init();
+ evbase = evsel->init();
}
- if (current_base->evbase == NULL)
- event_errx(1, "%s: no event mechanism available", __func__);
+ if (evbase == NULL)
+ errx(1, "%s: no event mechanism available", __func__);
if (!issetugid() && getenv("EVENT_SHOW_METHOD"))
- event_msgx("libevent using: %s\n",
- current_base->evsel->name);
-
- /* allocate a single active event queue */
- event_base_priority_init(current_base, 1);
-
- return (current_base);
-}
-
-int
-event_priority_init(int npriorities)
-{
- return event_base_priority_init(current_base, npriorities);
-}
-
-int
-event_base_priority_init(struct event_base *base, int npriorities)
-{
- int i;
-
- if (base->event_count_active)
- return (-1);
-
- if (base->nactivequeues && npriorities != base->nactivequeues) {
- for (i = 0; i < base->nactivequeues; ++i) {
- free(base->activequeues[i]);
- }
- free(base->activequeues);
- }
+ fprintf(stderr, "libevent using: %s\n", evsel->name);
- /* Allocate our priority queues */
- base->nactivequeues = npriorities;
- base->activequeues = (struct event_list **)calloc(base->nactivequeues,
- npriorities * sizeof(struct event_list *));
- if (base->activequeues == NULL)
- event_err(1, "%s: calloc", __func__);
-
- for (i = 0; i < base->nactivequeues; ++i) {
- base->activequeues[i] = malloc(sizeof(struct event_list));
- if (base->activequeues[i] == NULL)
- event_err(1, "%s: malloc", __func__);
- TAILQ_INIT(base->activequeues[i]);
- }
-
- return (0);
+#if defined(USE_LOG) && defined(USE_DEBUG)
+ log_to(stderr);
+ log_debug_cmd(LOG_MISC, 80);
+#endif
}
int
-event_haveevents(struct event_base *base)
+event_haveevents(void)
{
- return (base->event_count > 0);
+ return (RB_ROOT(&timetree) || TAILQ_FIRST(&eventqueue) ||
+ TAILQ_FIRST(&signalqueue) || TAILQ_FIRST(&activequeue));
}
-/*
- * Active events are stored in priority queues. Lower priorities are always
- * process before higher priorities. Low priority events can starve high
- * priority ones.
- */
-
static void
-event_process_active(struct event_base *base)
+event_process_active(void)
{
struct event *ev;
- struct event_list *activeq = NULL;
- int i;
short ncalls;
- if (!base->event_count_active)
- return;
-
- for (i = 0; i < base->nactivequeues; ++i) {
- if (TAILQ_FIRST(base->activequeues[i]) != NULL) {
- activeq = base->activequeues[i];
- break;
- }
- }
-
- for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
- event_queue_remove(base, ev, EVLIST_ACTIVE);
+ for (ev = TAILQ_FIRST(&activequeue); ev;
+ ev = TAILQ_FIRST(&activequeue)) {
+ event_queue_remove(ev, EVLIST_ACTIVE);
/* Allows deletes to work */
ncalls = ev->ev_ncalls;
@@ -273,64 +217,36 @@ event_dispatch(void)
return (event_loop(0));
}
-int
-event_base_dispatch(struct event_base *event_base)
-{
- return (event_base_loop(event_base, 0));
-}
-
static void
event_loopexit_cb(int fd, short what, void *arg)
{
- struct event_base *base = arg;
- base->event_gotterm = 1;
+ event_gotterm = 1;
}
-/* not thread safe */
-
int
event_loopexit(struct timeval *tv)
{
- return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
- current_base, tv));
+ return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, NULL, tv));
}
int
-event_base_loopexit(struct event_base *event_base, struct timeval *tv)
-{
- return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
- event_base, tv));
-}
-
-/* not thread safe */
-
-int
event_loop(int flags)
{
- return event_base_loop(current_base, flags);
-}
-
-int
-event_base_loop(struct event_base *base, int flags)
-{
- const struct eventop *evsel = base->evsel;
- void *evbase = base->evbase;
struct timeval tv;
int res, done;
/* Calculate the initial events that we are waiting for */
- if (evsel->recalc(base, evbase, 0) == -1)
+ if (evsel->recalc(evbase, 0) == -1)
return (-1);
done = 0;
while (!done) {
/* Terminate the loop if we have been asked to */
- if (base->event_gotterm) {
- base->event_gotterm = 0;
+ if (event_gotterm) {
+ event_gotterm = 0;
break;
}
- /* You cannot use this interface for multi-threaded apps */
while (event_gotsig) {
event_gotsig = 0;
if (event_sigcb) {
@@ -344,45 +260,44 @@ event_base_loop(struct event_base *base, int flags)
/* Check if time is running backwards */
gettimeofday(&tv, NULL);
- if (timercmp(&tv, &base->event_tv, <)) {
+ if (timercmp(&tv, &event_tv, <)) {
struct timeval off;
- event_debug(("%s: time is running backwards, corrected",
+ LOG_DBG((LOG_MISC, 10,
+ "%s: time is running backwards, corrected",
__func__));
- timersub(&base->event_tv, &tv, &off);
- timeout_correct(base, &off);
+
+ timersub(&event_tv, &tv, &off);
+ timeout_correct(&off);
}
- base->event_tv = tv;
+ event_tv = tv;
- if (!base->event_count_active && !(flags & EVLOOP_NONBLOCK))
- timeout_next(base, &tv);
+ if (!(flags & EVLOOP_NONBLOCK))
+ timeout_next(&tv);
else
timerclear(&tv);
/* If we have no events, we just exit */
- if (!event_haveevents(base)) {
- event_debug(("%s: no events registered.", __func__));
+ if (!event_haveevents())
return (1);
- }
- res = evsel->dispatch(base, evbase, &tv);
+ res = evsel->dispatch(evbase, &tv);
if (res == -1)
return (-1);
- timeout_process(base);
+ timeout_process();
- if (base->event_count_active) {
- event_process_active(base);
- if (!base->event_count_active && (flags & EVLOOP_ONCE))
+ if (TAILQ_FIRST(&activequeue)) {
+ event_process_active();
+ if (flags & EVLOOP_ONCE)
done = 1;
} else if (flags & EVLOOP_NONBLOCK)
done = 1;
- if (evsel->recalc(base, evbase, 0) == -1)
+ if (evsel->recalc(evbase, 0) == -1)
return (-1);
}
- event_debug(("%s: asked to terminate loop.", __func__));
return (0);
}
@@ -422,15 +337,15 @@ event_once(int fd, short events,
if ((eonce = calloc(1, sizeof(struct event_once))) == NULL)
return (-1);
- eonce->cb = callback;
- eonce->arg = arg;
-
if (events == EV_TIMEOUT) {
if (tv == NULL) {
timerclear(&etv);
tv = &etv;
}
+ eonce->cb = callback;
+ eonce->arg = arg;
+
evtimer_set(&eonce->ev, event_once_cb, eonce);
} else if (events & (EV_READ|EV_WRITE)) {
events &= EV_READ|EV_WRITE;
@@ -450,50 +365,18 @@ void
event_set(struct event *ev, int fd, short events,
void (*callback)(int, short, void *), void *arg)
{
- /* Take the current base - caller needs to set the real base later */
- ev->ev_base = current_base;
-
ev->ev_callback = callback;
ev->ev_arg = arg;
+#ifdef WIN32
+ ev->ev_fd = (HANDLE)fd;
+ ev->overlap.hEvent = ev;
+#else
ev->ev_fd = fd;
+#endif
ev->ev_events = events;
ev->ev_flags = EVLIST_INIT;
ev->ev_ncalls = 0;
ev->ev_pncalls = NULL;
-
- /* by default, we put new events into the middle priority */
- ev->ev_pri = current_base->nactivequeues/2;
-}
-
-int
-event_base_set(struct event_base *base, struct event *ev)
-{
- /* Only innocent events may be assigned to a different base */
- if (ev->ev_flags != EVLIST_INIT)
- return (-1);
-
- ev->ev_base = base;
- ev->ev_pri = current_base->nactivequeues/2;
-
- return (0);
-}
-
-/*
- * Set's the priority of an event - if an event is already scheduled
- * changing the priority is going to fail.
- */
-
-int
-event_priority_set(struct event *ev, int pri)
-{
- if (ev->ev_flags & EVLIST_ACTIVE)
- return (-1);
- if (pri < 0 || pri >= ev->ev_base->nactivequeues)
- return (-1);
-
- ev->ev_pri = pri;
-
- return (0);
}
/*
@@ -526,11 +409,7 @@ event_pending(struct event *ev, short event, struct timeval *tv)
int
event_add(struct event *ev, struct timeval *tv)
{
- struct event_base *base = ev->ev_base;
- const struct eventop *evsel = base->evsel;
- void *evbase = base->evbase;
-
- event_debug((
+ LOG_DBG((LOG_MISC, 55,
"event_add: event: %p, %s%s%scall %p",
ev,
ev->ev_events & EV_READ ? "EV_READ " : " ",
@@ -544,7 +423,7 @@ event_add(struct event *ev, struct timeval *tv)
struct timeval now;
if (ev->ev_flags & EVLIST_TIMEOUT)
- event_queue_remove(base, ev, EVLIST_TIMEOUT);
+ event_queue_remove(ev, EVLIST_TIMEOUT);
/* Check if it is active due to a timeout. Rescheduling
* this timeout before the callback can be executed
@@ -559,27 +438,27 @@ event_add(struct event *ev, struct timeval *tv)
*ev->ev_pncalls = 0;
}
- event_queue_remove(base, ev, EVLIST_ACTIVE);
+ event_queue_remove(ev, EVLIST_ACTIVE);
}
gettimeofday(&now, NULL);
timeradd(&now, tv, &ev->ev_timeout);
- event_debug((
+ LOG_DBG((LOG_MISC, 55,
"event_add: timeout in %d seconds, call %p",
tv->tv_sec, ev->ev_callback));
- event_queue_insert(base, ev, EVLIST_TIMEOUT);
+ event_queue_insert(ev, EVLIST_TIMEOUT);
}
if ((ev->ev_events & (EV_READ|EV_WRITE)) &&
!(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
- event_queue_insert(base, ev, EVLIST_INSERTED);
+ event_queue_insert(ev, EVLIST_INSERTED);
return (evsel->add(evbase, ev));
} else if ((ev->ev_events & EV_SIGNAL) &&
!(ev->ev_flags & EVLIST_SIGNAL)) {
- event_queue_insert(base, ev, EVLIST_SIGNAL);
+ event_queue_insert(ev, EVLIST_SIGNAL);
return (evsel->add(evbase, ev));
}
@@ -590,21 +469,9 @@ event_add(struct event *ev, struct timeval *tv)
int
event_del(struct event *ev)
{
- struct event_base *base;
- const struct eventop *evsel;
- void *evbase;
-
- event_debug(("event_del: %p, callback %p",
+ LOG_DBG((LOG_MISC, 80, "event_del: %p, callback %p",
ev, ev->ev_callback));
- /* An event without a base has not been added */
- if (ev->ev_base == NULL)
- return (-1);
-
- base = ev->ev_base;
- evsel = base->evsel;
- evbase = base->evbase;
-
assert(!(ev->ev_flags & ~EVLIST_ALL));
/* See if we are just active executing this event in a loop */
@@ -614,16 +481,16 @@ event_del(struct event *ev)
}
if (ev->ev_flags & EVLIST_TIMEOUT)
- event_queue_remove(base, ev, EVLIST_TIMEOUT);
+ event_queue_remove(ev, EVLIST_TIMEOUT);
if (ev->ev_flags & EVLIST_ACTIVE)
- event_queue_remove(base, ev, EVLIST_ACTIVE);
+ event_queue_remove(ev, EVLIST_ACTIVE);
if (ev->ev_flags & EVLIST_INSERTED) {
- event_queue_remove(base, ev, EVLIST_INSERTED);
+ event_queue_remove(ev, EVLIST_INSERTED);
return (evsel->del(evbase, ev));
} else if (ev->ev_flags & EVLIST_SIGNAL) {
- event_queue_remove(base, ev, EVLIST_SIGNAL);
+ event_queue_remove(ev, EVLIST_SIGNAL);
return (evsel->del(evbase, ev));
}
@@ -642,18 +509,18 @@ event_active(struct event *ev, int res, short ncalls)
ev->ev_res = res;
ev->ev_ncalls = ncalls;
ev->ev_pncalls = NULL;
- event_queue_insert(ev->ev_base, ev, EVLIST_ACTIVE);
+ event_queue_insert(ev, EVLIST_ACTIVE);
}
int
-timeout_next(struct event_base *base, struct timeval *tv)
+timeout_next(struct timeval *tv)
{
struct timeval dflt = TIMEOUT_DEFAULT;
struct timeval now;
struct event *ev;
- if ((ev = RB_MIN(event_tree, &base->timetree)) == NULL) {
+ if ((ev = RB_MIN(event_tree, &timetree)) == NULL) {
*tv = dflt;
return (0);
}
@@ -671,143 +538,96 @@ timeout_next(struct event_base *base, struct timeval *tv)
assert(tv->tv_sec >= 0);
assert(tv->tv_usec >= 0);
- event_debug(("timeout_next: in %d seconds", tv->tv_sec));
+ LOG_DBG((LOG_MISC, 60, "timeout_next: in %d seconds", tv->tv_sec));
return (0);
}
-static void
-timeout_correct(struct event_base *base, struct timeval *off)
+void
+timeout_correct(struct timeval *off)
{
struct event *ev;
- /*
- * We can modify the key element of the node without destroying
+ /* We can modify the key element of the node without destroying
* the key, beause we apply it to all in the right order.
*/
- RB_FOREACH(ev, event_tree, &base->timetree)
+ RB_FOREACH(ev, event_tree, &timetree)
timersub(&ev->ev_timeout, off, &ev->ev_timeout);
}
void
-timeout_process(struct event_base *base)
+timeout_process(void)
{
struct timeval now;
struct event *ev, *next;
gettimeofday(&now, NULL);
- for (ev = RB_MIN(event_tree, &base->timetree); ev; ev = next) {
+ for (ev = RB_MIN(event_tree, &timetree); ev; ev = next) {
if (timercmp(&ev->ev_timeout, &now, >))
break;
- next = RB_NEXT(event_tree, &base->timetree, ev);
+ next = RB_NEXT(event_tree, &timetree, ev);
- event_queue_remove(base, ev, EVLIST_TIMEOUT);
+ event_queue_remove(ev, EVLIST_TIMEOUT);
/* delete this event from the I/O queues */
event_del(ev);
- event_debug(("timeout_process: call %p",
+ LOG_DBG((LOG_MISC, 60, "timeout_process: call %p",
ev->ev_callback));
event_active(ev, EV_TIMEOUT, 1);
}
}
void
-event_queue_remove(struct event_base *base, struct event *ev, int queue)
+event_queue_remove(struct event *ev, int queue)
{
- int docount = 1;
-
if (!(ev->ev_flags & queue))
- event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
- ev, ev->ev_fd, queue);
-
- if (ev->ev_flags & EVLIST_INTERNAL)
- docount = 0;
-
- if (docount)
- base->event_count--;
+ errx(1, "%s: %p(fd %d) not on queue %x", __func__,
+ ev, ev->ev_fd, queue);
ev->ev_flags &= ~queue;
switch (queue) {
case EVLIST_ACTIVE:
- if (docount)
- base->event_count_active--;
- TAILQ_REMOVE(base->activequeues[ev->ev_pri],
- ev, ev_active_next);
+ TAILQ_REMOVE(&activequeue, ev, ev_active_next);
break;
case EVLIST_SIGNAL:
TAILQ_REMOVE(&signalqueue, ev, ev_signal_next);
break;
case EVLIST_TIMEOUT:
- RB_REMOVE(event_tree, &base->timetree, ev);
+ RB_REMOVE(event_tree, &timetree, ev);
break;
case EVLIST_INSERTED:
- TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
+ TAILQ_REMOVE(&eventqueue, ev, ev_next);
break;
default:
- event_errx(1, "%s: unknown queue %x", __func__, queue);
+ errx(1, "%s: unknown queue %x", __func__, queue);
}
}
void
-event_queue_insert(struct event_base *base, struct event *ev, int queue)
+event_queue_insert(struct event *ev, int queue)
{
- int docount = 1;
-
- if (ev->ev_flags & queue) {
- /* Double insertion is possible for active events */
- if (queue & EVLIST_ACTIVE)
- return;
-
- event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
- ev, ev->ev_fd, queue);
- }
-
- if (ev->ev_flags & EVLIST_INTERNAL)
- docount = 0;
-
- if (docount)
- base->event_count++;
+ if (ev->ev_flags & queue)
+ errx(1, "%s: %p(fd %d) already on queue %x", __func__,
+ ev, ev->ev_fd, queue);
ev->ev_flags |= queue;
switch (queue) {
case EVLIST_ACTIVE:
- if (docount)
- base->event_count_active++;
- TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri],
- ev,ev_active_next);
+ TAILQ_INSERT_TAIL(&activequeue, ev, ev_active_next);
break;
case EVLIST_SIGNAL:
TAILQ_INSERT_TAIL(&signalqueue, ev, ev_signal_next);
break;
case EVLIST_TIMEOUT: {
- struct event *tmp = RB_INSERT(event_tree, &base->timetree, ev);
+ struct event *tmp = RB_INSERT(event_tree, &timetree, ev);
assert(tmp == NULL);
break;
}
case EVLIST_INSERTED:
- TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
+ TAILQ_INSERT_TAIL(&eventqueue, ev, ev_next);
break;
default:
- event_errx(1, "%s: unknown queue %x", __func__, queue);
+ errx(1, "%s: unknown queue %x", __func__, queue);
}
}
-
-/* Functions for debugging */
-
-const char *
-event_get_version(void)
-{
- return (LIBEVENT_VERSION);
-}
-
-/*
- * No thread-safe interface needed - the information should be the same
- * for all threads.
- */
-
-const char *
-event_get_method(void)
-{
- return (current_base->evsel->name);
-}
diff --git a/lib/libevent/event.h b/lib/libevent/event.h
index e37d34f7631..70f04b7eb6b 100644
--- a/lib/libevent/event.h
+++ b/lib/libevent/event.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: event.h,v 1.7 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: event.h,v 1.8 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
@@ -34,23 +34,17 @@ extern "C" {
#endif
#ifdef WIN32
-#define WIN32_LEAN_AND_MEAN
#include <windows.h>
-#undef WIN32_LEAN_AND_MEAN
-typedef unsigned char u_char;
#endif
-#define LIBEVENT_VERSION "1.0c"
-
#define EVLIST_TIMEOUT 0x01
#define EVLIST_INSERTED 0x02
#define EVLIST_SIGNAL 0x04
#define EVLIST_ACTIVE 0x08
-#define EVLIST_INTERNAL 0x10
#define EVLIST_INIT 0x80
/* EVLIST_X_ Private space: 0x1000-0xf000 */
-#define EVLIST_ALL (0xf000 | 0x9f)
+#define EVLIST_ALL (0xf000 | 0x8f)
#define EV_TIMEOUT 0x01
#define EV_READ 0x02
@@ -78,23 +72,24 @@ struct { \
}
#endif /* !RB_ENTRY */
-struct event_base;
struct event {
TAILQ_ENTRY (event) ev_next;
TAILQ_ENTRY (event) ev_active_next;
TAILQ_ENTRY (event) ev_signal_next;
RB_ENTRY (event) ev_timeout_node;
- struct event_base *ev_base;
+#ifdef WIN32
+ HANDLE ev_fd;
+ OVERLAPPED overlap;
+#else
int ev_fd;
+#endif
short ev_events;
short ev_ncalls;
short *ev_pncalls; /* Allows deletes in callback */
struct timeval ev_timeout;
- int ev_pri; /* smaller numbers are higher priority */
-
void (*ev_callback)(int, short, void *arg);
void *ev_arg;
@@ -121,32 +116,23 @@ struct eventop {
void *(*init)(void);
int (*add)(void *, struct event *);
int (*del)(void *, struct event *);
- int (*recalc)(struct event_base *, void *, int);
- int (*dispatch)(struct event_base *, void *, struct timeval *);
+ int (*recalc)(void *, int);
+ int (*dispatch)(void *, struct timeval *);
};
#define TIMEOUT_DEFAULT {5, 0}
-void *event_init(void);
+void event_init(void);
int event_dispatch(void);
-int event_base_dispatch(struct event_base *);
-
-#define _EVENT_LOG_DEBUG 0
-#define _EVENT_LOG_MSG 1
-#define _EVENT_LOG_WARN 2
-#define _EVENT_LOG_ERR 3
-typedef void (*event_log_cb)(int severity, const char *msg);
-void event_set_log_callback(event_log_cb cb);
-
-/* Associate a different event base with an event */
-int event_base_set(struct event_base *, struct event *);
#define EVLOOP_ONCE 0x01
#define EVLOOP_NONBLOCK 0x02
int event_loop(int);
-int event_base_loop(struct event_base *, int);
int event_loopexit(struct timeval *); /* Causes the loop to exit */
-int event_base_loopexit(struct event_base *, struct timeval *);
+
+int timeout_next(struct timeval *);
+void timeout_correct(struct timeval *);
+void timeout_process(void);
#define evtimer_add(ev, tv) event_add(ev, tv)
#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
@@ -176,23 +162,11 @@ int event_pending(struct event *, short, struct timeval *);
#define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
#endif
-/* Some simple debugging functions */
-const char *event_get_version(void);
-const char *event_get_method(void);
-
-/* These functions deal with event priorities */
-
-int event_priority_init(int);
-int event_base_priority_init(struct event_base *, int);
-int event_priority_set(struct event *, int);
-
/* These functions deal with buffering input and output */
struct evbuffer {
u_char *buffer;
- u_char *orig_buffer;
- size_t misalign;
size_t totallen;
size_t off;
@@ -239,7 +213,6 @@ struct bufferevent {
struct bufferevent *bufferevent_new(int fd,
evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
-int bufferevent_priority_set(struct bufferevent *bufev, int pri);
void bufferevent_free(struct bufferevent *bufev);
int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
@@ -256,9 +229,7 @@ void bufferevent_settimeout(struct bufferevent *bufev,
struct evbuffer *evbuffer_new(void);
void evbuffer_free(struct evbuffer *);
-int evbuffer_expand(struct evbuffer *, size_t);
-int evbuffer_add(struct evbuffer *, void *, size_t);
-int evbuffer_remove(struct evbuffer *, void *, size_t);
+int evbuffer_add(struct evbuffer *, u_char *, size_t);
int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
int evbuffer_add_printf(struct evbuffer *, char *fmt, ...);
void evbuffer_drain(struct evbuffer *, size_t);
diff --git a/lib/libevent/kqueue.c b/lib/libevent/kqueue.c
index 0431c61bb47..78309cd6e8d 100644
--- a/lib/libevent/kqueue.c
+++ b/lib/libevent/kqueue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kqueue.c,v 1.13 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: kqueue.c,v 1.14 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
@@ -44,18 +44,29 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
+#include <err.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
-#if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
+#ifdef USE_LOG
+#include "log.h"
+#else
+#define LOG_DBG(x)
+#define log_error warn
+#endif
+
+#if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__)
#define INTPTR(x) (intptr_t)x
#else
#define INTPTR(x) x
#endif
#include "event.h"
-#include "log.h"
+
+extern struct event_list timequeue;
+extern struct event_list eventqueue;
+extern struct event_list addqueue;
#define EVLIST_X_KQINKERNEL 0x1000
@@ -67,13 +78,13 @@ struct kqop {
struct kevent *events;
int nevents;
int kq;
-};
+} kqueueop;
void *kq_init (void);
int kq_add (void *, struct event *);
int kq_del (void *, struct event *);
-int kq_recalc (struct event_base *, void *, int);
-int kq_dispatch (struct event_base *, void *, struct timeval *);
+int kq_recalc (void *, int);
+int kq_dispatch (void *, struct timeval *);
int kq_insert (struct kqop *, struct kevent *);
const struct eventop kqops = {
@@ -89,44 +100,38 @@ void *
kq_init(void)
{
int kq;
- struct kqop *kqueueop;
/* Disable kqueue when this environment variable is set */
if (!issetugid() && getenv("EVENT_NOKQUEUE"))
return (NULL);
- if (!(kqueueop = calloc(1, sizeof(struct kqop))))
- return (NULL);
+ memset(&kqueueop, 0, sizeof(kqueueop));
/* Initalize the kernel queue */
if ((kq = kqueue()) == -1) {
- event_warn("kqueue");
- free (kqueueop);
+ log_error("kqueue");
return (NULL);
}
- kqueueop->kq = kq;
+ kqueueop.kq = kq;
/* Initalize fields */
- kqueueop->changes = malloc(NEVENT * sizeof(struct kevent));
- if (kqueueop->changes == NULL) {
- free (kqueueop);
+ kqueueop.changes = malloc(NEVENT * sizeof(struct kevent));
+ if (kqueueop.changes == NULL)
return (NULL);
- }
- kqueueop->events = malloc(NEVENT * sizeof(struct kevent));
- if (kqueueop->events == NULL) {
- free (kqueueop->changes);
- free (kqueueop);
+ kqueueop.events = malloc(NEVENT * sizeof(struct kevent));
+ if (kqueueop.events == NULL) {
+ free (kqueueop.changes);
return (NULL);
}
- kqueueop->nevents = NEVENT;
+ kqueueop.nevents = NEVENT;
- return (kqueueop);
+ return (&kqueueop);
}
int
-kq_recalc(struct event_base *base, void *arg, int max)
+kq_recalc(void *arg, int max)
{
return (0);
}
@@ -145,7 +150,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
newchange = realloc(kqop->changes,
nevents * sizeof(struct kevent));
if (newchange == NULL) {
- event_warn("%s: malloc", __func__);
+ log_error("%s: malloc", __func__);
return (-1);
}
kqop->changes = newchange;
@@ -158,7 +163,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
* the next realloc will pick it up.
*/
if (newresult == NULL) {
- event_warn("%s: malloc", __func__);
+ log_error("%s: malloc", __func__);
return (-1);
}
kqop->events = newresult;
@@ -168,7 +173,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent));
- event_debug(("%s: fd %d %s%s",
+ LOG_DBG((LOG_MISC, 70, "%s: fd %d %s%s",
__func__, kev->ident,
kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE",
kev->flags == EV_DELETE ? " (del)" : ""));
@@ -183,7 +188,7 @@ kq_sighandler(int sig)
}
int
-kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
+kq_dispatch(void *arg, struct timeval *tv)
{
struct kqop *kqop = arg;
struct kevent *changes = kqop->changes;
@@ -199,14 +204,14 @@ kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
kqop->nchanges = 0;
if (res == -1) {
if (errno != EINTR) {
- event_warn("kevent");
+ log_error("kevent");
return (-1);
}
return (0);
}
- event_debug(("%s: kevent reports %d", __func__, res));
+ LOG_DBG((LOG_MISC, 80, "%s: kevent reports %d", __func__, res));
for (i = 0; i < res; i++) {
int which = 0;
diff --git a/lib/libevent/log.c b/lib/libevent/log.c
deleted file mode 100644
index b70ef1c3f69..00000000000
--- a/lib/libevent/log.c
+++ /dev/null
@@ -1,208 +0,0 @@
-/* $OpenBSD: log.c,v 1.1 2005/04/19 02:03:12 brad Exp $ */
-
-/*
- * log.c
- *
- * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
- *
- * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net>
- *
- * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
- *
- * Copyright (c) 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#ifdef WIN32
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#undef WIN32_LEAN_AND_MEAN
-#include "misc.h"
-#endif
-#include <sys/types.h>
-#include <sys/tree.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#else
-#include <sys/_time.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <errno.h>
-#include "event.h"
-
-#include "log.h"
-
-static void _warn_helper(int severity, int log_errno, const char *fmt,
- va_list ap);
-static void event_log(int severity, const char *msg);
-
-static int
-event_vsnprintf(char *str, size_t size, const char *format, va_list args)
-{
- int r;
- if (size == 0)
- return -1;
-#ifdef WIN32
- r = _vsnprintf(str, size, format, args);
-#else
- r = vsnprintf(str, size, format, args);
-#endif
- str[size-1] = '\0';
- if (r < 0 || ((size_t)r) >= size) {
- /* different platforms behave differently on overflow;
- * handle both kinds. */
- return -1;
- }
- return r;
-}
-
-void
-event_err(int eval, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap);
- va_end(ap);
- exit(eval);
-}
-
-void
-event_warn(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap);
- va_end(ap);
-}
-
-void
-event_errx(int eval, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap);
- va_end(ap);
- exit(eval);
-}
-
-void
-event_warnx(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap);
- va_end(ap);
-}
-
-void
-event_msgx(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap);
- va_end(ap);
-}
-
-void
-_event_debugx(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap);
- va_end(ap);
-}
-
-static void
-_warn_helper(int severity, int log_errno, const char *fmt, va_list ap)
-{
- char buf[1024];
- size_t len;
-
- if (fmt != NULL)
- event_vsnprintf(buf, sizeof(buf), fmt, ap);
- else
- buf[0] = '\0';
-
- if (log_errno >= 0) {
- len = strlen(buf);
- if (len < sizeof(buf) - 3) {
- snprintf(buf + len, sizeof(buf) - len, ": %s",
- strerror(log_errno));
- }
- }
-
- event_log(severity, buf);
-}
-
-static event_log_cb log_fn = NULL;
-
-void
-event_set_log_callback(event_log_cb cb)
-{
- log_fn = cb;
-}
-
-static void
-event_log(int severity, const char *msg)
-{
- if (log_fn)
- log_fn(severity, msg);
- else {
- const char *severity_str;
- switch (severity) {
- case _EVENT_LOG_DEBUG:
- severity_str = "debug";
- break;
- case _EVENT_LOG_MSG:
- severity_str = "msg";
- break;
- case _EVENT_LOG_WARN:
- severity_str = "warn";
- break;
- case _EVENT_LOG_ERR:
- severity_str = "err";
- break;
- default:
- severity_str = "???";
- break;
- }
- (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
- }
-}
diff --git a/lib/libevent/log.h b/lib/libevent/log.h
deleted file mode 100644
index 84e83ae9daf..00000000000
--- a/lib/libevent/log.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/* $OpenBSD: log.h,v 1.1 2005/04/19 02:03:12 brad Exp $ */
-
-/*
- * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-#ifndef _LOG_H_
-#define _LOG_H_
-
-void event_err(int eval, const char *fmt, ...);
-void event_warn(const char *fmt, ...);
-void event_errx(int eval, const char *fmt, ...);
-void event_warnx(const char *fmt, ...);
-void event_msgx(const char *fmt, ...);
-void _event_debugx(const char *fmt, ...);
-#undef USE_DEBUG
-#ifdef USE_DEBUG
-#define event_debug(x) _event_debugx x
-#else
-#define event_debug(x)
-#endif
-
-#endif
diff --git a/lib/libevent/poll.c b/lib/libevent/poll.c
index 0bdda02736e..d1f7e1628b9 100644
--- a/lib/libevent/poll.c
+++ b/lib/libevent/poll.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: poll.c,v 1.4 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: poll.c,v 1.5 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>
@@ -37,7 +37,6 @@
#include <sys/_time.h>
#endif
#include <sys/queue.h>
-#include <sys/tree.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
@@ -45,11 +44,19 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
+#include <err.h>
+
+#ifdef USE_LOG
+#include "log.h"
+#else
+#define LOG_DBG(x)
+#define log_error(x) perror(x)
+#endif
#include "event.h"
-#include "event-internal.h"
#include "evsignal.h"
-#include "log.h"
+
+extern struct event_list eventqueue;
extern volatile sig_atomic_t evsignal_caught;
@@ -58,13 +65,13 @@ struct pollop {
struct pollfd *event_set;
struct event **event_back;
sigset_t evsigmask;
-};
+} pollop;
void *poll_init (void);
int poll_add (void *, struct event *);
int poll_del (void *, struct event *);
-int poll_recalc (struct event_base *, void *, int);
-int poll_dispatch (struct event_base *, void *, struct timeval *);
+int poll_recalc (void *, int);
+int poll_dispatch (void *, struct timeval *);
struct eventop pollops = {
"poll",
@@ -78,18 +85,15 @@ struct eventop pollops = {
void *
poll_init(void)
{
- struct pollop *pollop;
-
/* Disable kqueue when this environment variable is set */
if (!issetugid() && getenv("EVENT_NOPOLL"))
return (NULL);
- if (!(pollop = calloc(1, sizeof(struct pollop))))
- return (NULL);
+ memset(&pollop, 0, sizeof(pollop));
- evsignal_init(&pollop->evsigmask);
+ evsignal_init(&pollop.evsigmask);
- return (pollop);
+ return (&pollop);
}
/*
@@ -98,7 +102,7 @@ poll_init(void)
*/
int
-poll_recalc(struct event_base *base, void *arg, int max)
+poll_recalc(void *arg, int max)
{
struct pollop *pop = arg;
@@ -106,7 +110,7 @@ poll_recalc(struct event_base *base, void *arg, int max)
}
int
-poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
+poll_dispatch(void *arg, struct timeval *tv)
{
int res, i, count, sec, nfds;
struct event *ev;
@@ -114,7 +118,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
count = pop->event_count;
nfds = 0;
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
+ TAILQ_FOREACH(ev, &eventqueue, ev_next) {
if (nfds + 1 >= count) {
if (count < 32)
count = 32;
@@ -125,13 +129,13 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
pop->event_set = realloc(pop->event_set,
count * sizeof(struct pollfd));
if (pop->event_set == NULL) {
- event_warn("realloc");
+ log_error("realloc");
return (-1);
}
pop->event_back = realloc(pop->event_back,
count * sizeof(struct event *));
if (pop->event_back == NULL) {
- event_warn("realloc");
+ log_error("realloc");
return (-1);
}
pop->event_count = count;
@@ -162,7 +166,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
if (evsignal_deliver(&pop->evsigmask) == -1)
return (-1);
- sec = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
+ sec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
res = poll(pop->event_set, nfds, sec);
if (evsignal_recalc(&pop->evsigmask) == -1)
@@ -170,7 +174,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
if (res == -1) {
if (errno != EINTR) {
- event_warn("poll");
+ log_error("poll");
return (-1);
}
@@ -179,7 +183,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
} else if (evsignal_caught)
evsignal_process();
- event_debug(("%s: poll reports %d", __func__, res));
+ LOG_DBG((LOG_MISC, 80, "%s: poll reports %d", __func__, res));
if (res == 0)
return (0);
diff --git a/lib/libevent/select.c b/lib/libevent/select.c
index dd2d9573a17..fb692729d80 100644
--- a/lib/libevent/select.c
+++ b/lib/libevent/select.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: select.c,v 1.7 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: select.c,v 1.8 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
@@ -37,18 +37,25 @@
#include <sys/_time.h>
#endif
#include <sys/queue.h>
-#include <sys/tree.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
+#include <err.h>
+
+#ifdef USE_LOG
+#include "log.h"
+#else
+#define LOG_DBG(x)
+#define log_error(x) perror(x)
+#endif
#include "event.h"
-#include "event-internal.h"
#include "evsignal.h"
-#include "log.h"
+
+extern struct event_list eventqueue;
#ifndef howmany
#define howmany(x, y) (((x)+((y)-1))/(y))
@@ -62,13 +69,13 @@ struct selectop {
fd_set *event_readset;
fd_set *event_writeset;
sigset_t evsigmask;
-};
+} sop;
void *select_init (void);
int select_add (void *, struct event *);
int select_del (void *, struct event *);
-int select_recalc (struct event_base *, void *, int);
-int select_dispatch (struct event_base *, void *, struct timeval *);
+int select_recalc (void *, int);
+int select_dispatch (void *, struct timeval *);
const struct eventop selectops = {
"select",
@@ -82,18 +89,15 @@ const struct eventop selectops = {
void *
select_init(void)
{
- struct selectop *sop;
-
/* Disable kqueue when this environment variable is set */
if (!issetugid() && getenv("EVENT_NOSELECT"))
return (NULL);
- if (!(sop = calloc(1, sizeof(struct selectop))))
- return (NULL);
+ memset(&sop, 0, sizeof(sop));
- evsignal_init(&sop->evsigmask);
+ evsignal_init(&sop.evsigmask);
- return (sop);
+ return (&sop);
}
/*
@@ -102,7 +106,7 @@ select_init(void)
*/
int
-select_recalc(struct event_base *base, void *arg, int max)
+select_recalc(void *arg, int max)
{
struct selectop *sop = arg;
fd_set *readset, *writeset;
@@ -113,7 +117,7 @@ select_recalc(struct event_base *base, void *arg, int max)
sop->event_fds = max;
if (!sop->event_fds) {
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next)
+ TAILQ_FOREACH(ev, &eventqueue, ev_next)
if (ev->ev_fd > sop->event_fds)
sop->event_fds = ev->ev_fd;
}
@@ -121,12 +125,12 @@ select_recalc(struct event_base *base, void *arg, int max)
fdsz = howmany(sop->event_fds + 1, NFDBITS) * sizeof(fd_mask);
if (fdsz > sop->event_fdsz) {
if ((readset = realloc(sop->event_readset, fdsz)) == NULL) {
- event_warn("malloc");
+ log_error("malloc");
return (-1);
}
if ((writeset = realloc(sop->event_writeset, fdsz)) == NULL) {
- event_warn("malloc");
+ log_error("malloc");
free(readset);
return (-1);
}
@@ -145,7 +149,7 @@ select_recalc(struct event_base *base, void *arg, int max)
}
int
-select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
+select_dispatch(void *arg, struct timeval *tv)
{
int maxfd, res;
struct event *ev, *next;
@@ -154,7 +158,7 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
memset(sop->event_readset, 0, sop->event_fdsz);
memset(sop->event_writeset, 0, sop->event_fdsz);
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
+ TAILQ_FOREACH(ev, &eventqueue, ev_next) {
if (ev->ev_events & EV_WRITE)
FD_SET(ev->ev_fd, sop->event_writeset);
if (ev->ev_events & EV_READ)
@@ -172,7 +176,7 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
if (res == -1) {
if (errno != EINTR) {
- event_warn("select");
+ log_error("select");
return (-1);
}
@@ -181,10 +185,10 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
} else if (evsignal_caught)
evsignal_process();
- event_debug(("%s: select reports %d", __func__, res));
+ LOG_DBG((LOG_MISC, 80, "%s: select reports %d", __func__, res));
maxfd = 0;
- for (ev = TAILQ_FIRST(&base->eventqueue); ev != NULL; ev = next) {
+ for (ev = TAILQ_FIRST(&eventqueue); ev != NULL; ev = next) {
next = TAILQ_NEXT(ev, ev_next);
res = 0;
@@ -197,8 +201,6 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
if (res) {
if (!(ev->ev_events & EV_PERSIST))
event_del(ev);
- else if (ev->ev_fd > maxfd)
- maxfd = ev->ev_fd;
event_active(ev, res, 1);
} else if (ev->ev_fd > maxfd)
maxfd = ev->ev_fd;
diff --git a/lib/libevent/signal.c b/lib/libevent/signal.c
index 3105d11f131..2bd98148760 100644
--- a/lib/libevent/signal.c
+++ b/lib/libevent/signal.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: signal.c,v 1.4 2005/04/19 02:03:12 brad Exp $ */
+/* $OpenBSD: signal.c,v 1.5 2005/04/19 08:07:45 deraadt Exp $ */
/*
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
@@ -37,20 +37,23 @@
#include <sys/_time.h>
#endif
#include <sys/queue.h>
-#include <sys/socket.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
+#include <err.h>
+
+#ifdef USE_LOG
+#include "log.h"
+#else
+#define LOG_DBG(x)
+#define log_error(x) perror(x)
#endif
#include "event.h"
#include "evsignal.h"
-#include "log.h"
extern struct event_list signalqueue;
@@ -58,63 +61,26 @@ static short evsigcaught[NSIG];
static int needrecalc;
volatile sig_atomic_t evsignal_caught = 0;
-static struct event ev_signal;
-static int ev_signal_pair[2];
-static int ev_signal_added;
-
-/* Callback for when the signal handler write a byte to our signaling socket */
-static void evsignal_cb(int fd, short what, void *arg)
-{
- static char signals[100];
- struct event *ev = arg;
- int n;
-
- n = read(fd, signals, sizeof(signals));
- if (n == -1)
- event_err(1, "%s: read", __func__);
- event_add(ev, NULL);
-}
-
-#ifdef HAVE_SETFD
-#define FD_CLOSEONEXEC(x) do { \
- if (fcntl(x, F_SETFD, 1) == -1) \
- event_warn("fcntl(%d, F_SETFD)", x); \
-} while (0)
-#else
-#define FD_CLOSEONEXEC(x)
-#endif
+void evsignal_process(void);
+int evsignal_recalc(sigset_t *);
+int evsignal_deliver(sigset_t *);
void
evsignal_init(sigset_t *evsigmask)
{
sigemptyset(evsigmask);
-
- /*
- * Our signal handler is going to write to one end of the socket
- * pair to wake up our event loop. The event loop then scans for
- * signals that got delivered.
- */
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1)
- event_err(1, "%s: socketpair", __func__);
-
- FD_CLOSEONEXEC(ev_signal_pair[0]);
- FD_CLOSEONEXEC(ev_signal_pair[1]);
-
- event_set(&ev_signal, ev_signal_pair[1], EV_READ,
- evsignal_cb, &ev_signal);
- ev_signal.ev_flags |= EVLIST_INTERNAL;
}
int
evsignal_add(sigset_t *evsigmask, struct event *ev)
{
int evsignal;
-
+
if (ev->ev_events & (EV_READ|EV_WRITE))
- event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
+ errx(1, "%s: EV_SIGNAL incompatible use", __func__);
evsignal = EVENT_SIGNAL(ev);
sigaddset(evsigmask, evsignal);
-
+
return (0);
}
@@ -139,9 +105,6 @@ evsignal_handler(int sig)
{
evsigcaught[sig]++;
evsignal_caught = 1;
-
- /* Wake up our notification mechanism */
- write(ev_signal_pair[0], "a", 1);
}
int
@@ -150,24 +113,19 @@ evsignal_recalc(sigset_t *evsigmask)
struct sigaction sa;
struct event *ev;
- if (!ev_signal_added) {
- ev_signal_added = 1;
- event_add(&ev_signal, NULL);
- }
-
if (TAILQ_FIRST(&signalqueue) == NULL && !needrecalc)
return (0);
needrecalc = 0;
if (sigprocmask(SIG_BLOCK, evsigmask, NULL) == -1)
return (-1);
-
+
/* Reinstall our signal handler. */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = evsignal_handler;
sa.sa_mask = *evsigmask;
sa.sa_flags |= SA_RESTART;
-
+
TAILQ_FOREACH(ev, &signalqueue, ev_signal_next) {
if (sigaction(EVENT_SIGNAL(ev), &sa, NULL) == -1)
return (-1);