mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-18 16:54:42 -04:00
debugging callbacks from Nick Mathewson <nickm@freehaven.net>
svn:r136
This commit is contained in:
parent
99442c6f00
commit
fbdaf3ab62
@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = foreign no-dependencies
|
||||
|
||||
SUBDIRS = . sample test
|
||||
|
||||
EXTRA_DIST = acconfig.h err.c event.h event-internal.h evsignal.h event.3 \
|
||||
EXTRA_DIST = acconfig.h event.h event-internal.h log.h evsignal.h event.3 \
|
||||
kqueue.c epoll_sub.c epoll.c select.c rtsig.c poll.c signal.c \
|
||||
devpoll.c \
|
||||
sample/Makefile.am sample/Makefile.in sample/event-test.c \
|
||||
@ -10,7 +10,7 @@ EXTRA_DIST = acconfig.h err.c event.h event-internal.h evsignal.h event.3 \
|
||||
test/Makefile.am test/Makefile.in test/bench.c test/regress.c \
|
||||
test/test-eof.c test/test-weof.c test/test-time.c \
|
||||
test/test-init.c test/test.sh \
|
||||
compat/err.h compat/sys/queue.h compat/sys/tree.h compat/sys/_time.h \
|
||||
compat/sys/queue.h compat/sys/tree.h compat/sys/_time.h \
|
||||
WIN32-Code WIN32-Code/config.h WIN32-Code/misc.c \
|
||||
WIN32-Code/win32.c WIN32-Code/misc.h \
|
||||
WIN32-Prj WIN32-Prj/event_test WIN32-Prj/event_test/event_test.dsp \
|
||||
@ -21,7 +21,7 @@ EXTRA_DIST = acconfig.h err.c event.h event-internal.h evsignal.h event.3 \
|
||||
|
||||
lib_LIBRARIES = libevent.a
|
||||
|
||||
libevent_a_SOURCES = event.c buffer.c evbuffer.c
|
||||
libevent_a_SOURCES = event.c buffer.c evbuffer.c log.c
|
||||
libevent_a_LIBADD = @LIBOBJS@
|
||||
|
||||
include_HEADERS = event.h
|
||||
|
1
buffer.c
1
buffer.c
@ -39,7 +39,6 @@
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -299,8 +299,6 @@ if test "x$needsignal" = "xyes" ; then
|
||||
AC_LIBOBJ(signal)
|
||||
fi
|
||||
|
||||
AC_REPLACE_FUNCS(err)
|
||||
|
||||
AC_TYPE_PID_T
|
||||
AC_TYPE_SIZE_T
|
||||
AC_CHECK_TYPE(u_int64_t, unsigned long long)
|
||||
|
17
devpoll.c
17
devpoll.c
@ -44,18 +44,11 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef USE_LOG
|
||||
#include "log.h"
|
||||
#else
|
||||
#define LOG_DBG(x)
|
||||
#define log_error warn
|
||||
#endif
|
||||
|
||||
#include "event.h"
|
||||
#include "evsignal.h"
|
||||
#include "log.h"
|
||||
|
||||
extern volatile sig_atomic_t evsignal_caught;
|
||||
|
||||
@ -113,7 +106,7 @@ devpoll_init(void)
|
||||
|
||||
/* Initialize the kernel queue */
|
||||
if ((dpfd = open("/dev/poll", O_RDWR)) == -1) {
|
||||
log_error("open: /dev/poll");
|
||||
event_warn("open: /dev/poll");
|
||||
free(devpollop);
|
||||
return (NULL);
|
||||
}
|
||||
@ -158,7 +151,7 @@ devpoll_recalc(struct event_base *base, void *arg, int max)
|
||||
|
||||
fds = realloc(devpollop->fds, nfds * sizeof(struct evdevpoll));
|
||||
if (fds == NULL) {
|
||||
log_error("realloc");
|
||||
event_warn("realloc");
|
||||
return (-1);
|
||||
}
|
||||
devpollop->fds = fds;
|
||||
@ -195,7 +188,7 @@ devpoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
log_error("ioctl: DP_POLL");
|
||||
event_warn("ioctl: DP_POLL");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@ -204,7 +197,7 @@ devpoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
} else if (evsignal_caught)
|
||||
evsignal_process();
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "%s: devpoll_wait reports %d", __func__, res));
|
||||
event_debug(("%s: devpoll_wait reports %d", __func__, res));
|
||||
|
||||
for (i = 0; i < res; i++) {
|
||||
int which = 0;
|
||||
|
19
epoll.c
19
epoll.c
@ -44,20 +44,13 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
#ifdef HAVE_FCNTL_H
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_LOG
|
||||
#include "log.h"
|
||||
#else
|
||||
#define LOG_DBG(x)
|
||||
#define log_error warn
|
||||
#endif
|
||||
|
||||
#include "event.h"
|
||||
#include "evsignal.h"
|
||||
#include "log.h"
|
||||
|
||||
extern volatile sig_atomic_t evsignal_caught;
|
||||
|
||||
@ -96,7 +89,7 @@ struct eventop epollops = {
|
||||
#ifdef HAVE_SETFD
|
||||
#define FD_CLOSEONEXEC(x) do { \
|
||||
if (fcntl(x, F_SETFD, 1) == -1) \
|
||||
warn("fcntl(%d, F_SETFD)", x); \
|
||||
event_warn("fcntl(%d, F_SETFD)", x); \
|
||||
} while (0)
|
||||
#else
|
||||
#define FD_CLOSEONEXEC(x)
|
||||
@ -122,7 +115,7 @@ epoll_init(void)
|
||||
/* Initalize the kernel queue */
|
||||
|
||||
if ((epfd = epoll_create(nfiles)) == -1) {
|
||||
log_error("epoll_create");
|
||||
event_warn("epoll_create");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@ -169,7 +162,7 @@ epoll_recalc(struct event_base *base, void *arg, int max)
|
||||
|
||||
fds = realloc(epollop->fds, nfds * sizeof(struct evepoll));
|
||||
if (fds == NULL) {
|
||||
log_error("realloc");
|
||||
event_warn("realloc");
|
||||
return (-1);
|
||||
}
|
||||
epollop->fds = fds;
|
||||
@ -200,7 +193,7 @@ epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
log_error("epoll_wait");
|
||||
event_warn("epoll_wait");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@ -209,7 +202,7 @@ epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
} else if (evsignal_caught)
|
||||
evsignal_process();
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "%s: epoll_wait reports %d", __func__, res));
|
||||
event_debug(("%s: epoll_wait reports %d", __func__, res));
|
||||
|
||||
for (i = 0; i < res; i++) {
|
||||
int which = 0;
|
||||
|
52
event.c
52
event.c
@ -47,18 +47,11 @@
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <err.h>
|
||||
#include <assert.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 "log.h"
|
||||
|
||||
#ifdef HAVE_SELECT
|
||||
extern const struct eventop selectops;
|
||||
@ -153,17 +146,12 @@ event_init(void)
|
||||
int i;
|
||||
|
||||
if ((current_base = calloc(1, sizeof(struct event_base))) == NULL)
|
||||
err(1, "%s: calloc");
|
||||
event_err(1, "%s: calloc");
|
||||
|
||||
event_sigcb = NULL;
|
||||
event_gotsig = 0;
|
||||
gettimeofday(¤t_base->event_tv, NULL);
|
||||
|
||||
#if defined(USE_LOG) && defined(USE_DEBUG)
|
||||
log_to(stderr);
|
||||
log_debug_cmd(LOG_MISC, 80);
|
||||
#endif
|
||||
|
||||
RB_INIT(¤t_base->timetree);
|
||||
TAILQ_INIT(¤t_base->eventqueue);
|
||||
TAILQ_INIT(&signalqueue);
|
||||
@ -176,11 +164,11 @@ event_init(void)
|
||||
}
|
||||
|
||||
if (current_base->evbase == NULL)
|
||||
errx(1, "%s: no event mechanism available", __func__);
|
||||
event_errx(1, "%s: no event mechanism available", __func__);
|
||||
|
||||
if (getenv("EVENT_SHOW_METHOD"))
|
||||
fprintf(stderr, "libevent using: %s\n",
|
||||
current_base->evsel->name);
|
||||
event_msgx("libevent using: %s\n",
|
||||
current_base->evsel->name);
|
||||
|
||||
/* allocate a single active event queue */
|
||||
event_base_priority_init(current_base, 1);
|
||||
@ -214,12 +202,12 @@ event_base_priority_init(struct event_base *base, int npriorities)
|
||||
base->activequeues = (struct event_list **)calloc(base->nactivequeues,
|
||||
npriorities * sizeof(struct event_list *));
|
||||
if (base->activequeues == NULL)
|
||||
err(1, "%s: calloc", __func__);
|
||||
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)
|
||||
err(1, "%s: malloc", __func__);
|
||||
event_err(1, "%s: malloc", __func__);
|
||||
TAILQ_INIT(base->activequeues[i]);
|
||||
}
|
||||
|
||||
@ -346,10 +334,8 @@ event_base_loop(struct event_base *base, int flags)
|
||||
gettimeofday(&tv, NULL);
|
||||
if (timercmp(&tv, &base->event_tv, <)) {
|
||||
struct timeval off;
|
||||
LOG_DBG((LOG_MISC, 10,
|
||||
"%s: time is running backwards, corrected",
|
||||
event_debug(("%s: time is running backwards, corrected",
|
||||
__func__));
|
||||
|
||||
timersub(&base->event_tv, &tv, &off);
|
||||
timeout_correct(base, &off);
|
||||
}
|
||||
@ -529,7 +515,7 @@ event_add(struct event *ev, struct timeval *tv)
|
||||
const struct eventop *evsel = base->evsel;
|
||||
void *evbase = base->evbase;
|
||||
|
||||
LOG_DBG((LOG_MISC, 55,
|
||||
event_debug((
|
||||
"event_add: event: %p, %s%s%scall %p",
|
||||
ev,
|
||||
ev->ev_events & EV_READ ? "EV_READ " : " ",
|
||||
@ -564,7 +550,7 @@ event_add(struct event *ev, struct timeval *tv)
|
||||
gettimeofday(&now, NULL);
|
||||
timeradd(&now, tv, &ev->ev_timeout);
|
||||
|
||||
LOG_DBG((LOG_MISC, 55,
|
||||
event_debug((
|
||||
"event_add: timeout in %d seconds, call %p",
|
||||
tv->tv_sec, ev->ev_callback));
|
||||
|
||||
@ -593,7 +579,7 @@ event_del(struct event *ev)
|
||||
const struct eventop *evsel;
|
||||
void *evbase;
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "event_del: %p, callback %p",
|
||||
event_debug(("event_del: %p, callback %p",
|
||||
ev, ev->ev_callback));
|
||||
|
||||
/* An event without a base has not been added */
|
||||
@ -670,7 +656,7 @@ timeout_next(struct event_base *base, struct timeval *tv)
|
||||
assert(tv->tv_sec >= 0);
|
||||
assert(tv->tv_usec >= 0);
|
||||
|
||||
LOG_DBG((LOG_MISC, 60, "timeout_next: in %d seconds", tv->tv_sec));
|
||||
event_debug(("timeout_next: in %d seconds", tv->tv_sec));
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -705,7 +691,7 @@ timeout_process(struct event_base *base)
|
||||
/* delete this event from the I/O queues */
|
||||
event_del(ev);
|
||||
|
||||
LOG_DBG((LOG_MISC, 60, "timeout_process: call %p",
|
||||
event_debug(("timeout_process: call %p",
|
||||
ev->ev_callback));
|
||||
event_active(ev, EV_TIMEOUT, 1);
|
||||
}
|
||||
@ -717,8 +703,8 @@ event_queue_remove(struct event_base *base, struct event *ev, int queue)
|
||||
int docount = 1;
|
||||
|
||||
if (!(ev->ev_flags & queue))
|
||||
errx(1, "%s: %p(fd %d) not on queue %x", __func__,
|
||||
ev, ev->ev_fd, 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;
|
||||
@ -744,7 +730,7 @@ event_queue_remove(struct event_base *base, struct event *ev, int queue)
|
||||
TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
|
||||
break;
|
||||
default:
|
||||
errx(1, "%s: unknown queue %x", __func__, queue);
|
||||
event_errx(1, "%s: unknown queue %x", __func__, queue);
|
||||
}
|
||||
}
|
||||
|
||||
@ -758,8 +744,8 @@ event_queue_insert(struct event_base *base, struct event *ev, int queue)
|
||||
if (queue & EVLIST_ACTIVE)
|
||||
return;
|
||||
|
||||
errx(1, "%s: %p(fd %d) already on queue %x", __func__,
|
||||
ev, ev->ev_fd, queue);
|
||||
event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
|
||||
ev, ev->ev_fd, queue);
|
||||
}
|
||||
|
||||
if (ev->ev_flags & EVLIST_INTERNAL)
|
||||
@ -788,7 +774,7 @@ event_queue_insert(struct event_base *base, struct event *ev, int queue)
|
||||
TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
|
||||
break;
|
||||
default:
|
||||
errx(1, "%s: unknown queue %x", __func__, queue);
|
||||
event_errx(1, "%s: unknown queue %x", __func__, queue);
|
||||
}
|
||||
}
|
||||
|
||||
|
7
event.h
7
event.h
@ -127,6 +127,13 @@ 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 *);
|
||||
|
||||
|
21
kqueue.c
21
kqueue.c
@ -44,18 +44,10 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
#ifdef HAVE_INTTYPES_H
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_LOG
|
||||
#include "log.h"
|
||||
#else
|
||||
#define LOG_DBG(x)
|
||||
#define log_error warn
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
|
||||
#define INTPTR(x) (intptr_t)x
|
||||
#else
|
||||
@ -63,6 +55,7 @@
|
||||
#endif
|
||||
|
||||
#include "event.h"
|
||||
#include "log.h"
|
||||
|
||||
#define EVLIST_X_KQINKERNEL 0x1000
|
||||
|
||||
@ -108,7 +101,7 @@ kq_init(void)
|
||||
/* Initalize the kernel queue */
|
||||
|
||||
if ((kq = kqueue()) == -1) {
|
||||
log_error("kqueue");
|
||||
event_warn("kqueue");
|
||||
free (kqueueop);
|
||||
return (NULL);
|
||||
}
|
||||
@ -152,7 +145,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
|
||||
newchange = realloc(kqop->changes,
|
||||
nevents * sizeof(struct kevent));
|
||||
if (newchange == NULL) {
|
||||
log_error("%s: malloc", __func__);
|
||||
event_warn("%s: malloc", __func__);
|
||||
return (-1);
|
||||
}
|
||||
kqop->changes = newchange;
|
||||
@ -165,7 +158,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
|
||||
* the next realloc will pick it up.
|
||||
*/
|
||||
if (newresult == NULL) {
|
||||
log_error("%s: malloc", __func__);
|
||||
event_warn("%s: malloc", __func__);
|
||||
return (-1);
|
||||
}
|
||||
kqop->events = newresult;
|
||||
@ -175,7 +168,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev)
|
||||
|
||||
memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent));
|
||||
|
||||
LOG_DBG((LOG_MISC, 70, "%s: fd %d %s%s",
|
||||
event_debug(("%s: fd %d %s%s",
|
||||
__func__, kev->ident,
|
||||
kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE",
|
||||
kev->flags == EV_DELETE ? " (del)" : ""));
|
||||
@ -206,14 +199,14 @@ kq_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
kqop->nchanges = 0;
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
log_error("kevent");
|
||||
event_warn("kevent");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "%s: kevent reports %d", __func__, res));
|
||||
event_debug(("%s: kevent reports %d", __func__, res));
|
||||
|
||||
for (i = 0; i < res; i++) {
|
||||
int which = 0;
|
||||
|
202
log.c
Normal file
202
log.c
Normal file
@ -0,0 +1,202 @@
|
||||
/* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey 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 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"
|
||||
|
||||
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) {
|
||||
event_vsnprintf(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);
|
||||
}
|
||||
}
|
43
log.h
Normal file
43
log.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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
|
17
poll.c
17
poll.c
@ -45,18 +45,11 @@
|
||||
#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 volatile sig_atomic_t evsignal_caught;
|
||||
|
||||
@ -132,13 +125,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) {
|
||||
log_error("realloc");
|
||||
event_warn("realloc");
|
||||
return (-1);
|
||||
}
|
||||
pop->event_back = realloc(pop->event_back,
|
||||
count * sizeof(struct event *));
|
||||
if (pop->event_back == NULL) {
|
||||
log_error("realloc");
|
||||
event_warn("realloc");
|
||||
return (-1);
|
||||
}
|
||||
pop->event_count = count;
|
||||
@ -177,7 +170,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
log_error("poll");
|
||||
event_warn("poll");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@ -186,7 +179,7 @@ poll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
} else if (evsignal_caught)
|
||||
evsignal_process();
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "%s: poll reports %d", __func__, res));
|
||||
event_debug(("%s: poll reports %d", __func__, res));
|
||||
|
||||
if (res == 0)
|
||||
return (0);
|
||||
|
3
rtsig.c
3
rtsig.c
@ -28,6 +28,7 @@
|
||||
#define EVLIST_X_NORT 0x1000 /* Skip RT signals (internal) */
|
||||
|
||||
#include "event.h"
|
||||
#include "log.h"
|
||||
extern struct event_list signalqueue;
|
||||
|
||||
struct rtsigop {
|
||||
@ -427,7 +428,7 @@ rtsig_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
op->toev = malloc(sizeof(*op->toev) * op->max);
|
||||
|
||||
if (op->poll == NULL || op->toev == NULL)
|
||||
err(1, "%s: malloc");
|
||||
event_err(1, "%s: malloc");
|
||||
}
|
||||
|
||||
return (0);
|
||||
|
16
select.c
16
select.c
@ -46,16 +46,10 @@
|
||||
#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"
|
||||
|
||||
#ifndef howmany
|
||||
#define howmany(x, y) (((x)+((y)-1))/(y))
|
||||
@ -128,12 +122,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) {
|
||||
log_error("malloc");
|
||||
event_warn("malloc");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((writeset = realloc(sop->event_writeset, fdsz)) == NULL) {
|
||||
log_error("malloc");
|
||||
event_warn("malloc");
|
||||
free(readset);
|
||||
return (-1);
|
||||
}
|
||||
@ -179,7 +173,7 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
log_error("select");
|
||||
event_warn("select");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@ -188,7 +182,7 @@ select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
|
||||
} else if (evsignal_caught)
|
||||
evsignal_process();
|
||||
|
||||
LOG_DBG((LOG_MISC, 80, "%s: select reports %d", __func__, res));
|
||||
event_debug(("%s: select reports %d", __func__, res));
|
||||
|
||||
maxfd = 0;
|
||||
for (ev = TAILQ_FIRST(&base->eventqueue); ev != NULL; ev = next) {
|
||||
|
17
signal.c
17
signal.c
@ -42,20 +42,13 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
#ifdef HAVE_FCNTL_H
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#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;
|
||||
|
||||
@ -80,14 +73,14 @@ static void evsignal_cb(int fd, short what, void *arg)
|
||||
|
||||
n = read(fd, signals, sizeof(signals));
|
||||
if (n == -1)
|
||||
err(1, "%s: read", __func__);
|
||||
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) \
|
||||
warn("fcntl(%d, F_SETFD)", x); \
|
||||
event_warn("fcntl(%d, F_SETFD)", x); \
|
||||
} while (0)
|
||||
#else
|
||||
#define FD_CLOSEONEXEC(x)
|
||||
@ -104,7 +97,7 @@ evsignal_init(sigset_t *evsigmask)
|
||||
* signals that got delivered.
|
||||
*/
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1)
|
||||
err(1, "%s: socketpair", __func__);
|
||||
event_err(1, "%s: socketpair", __func__);
|
||||
|
||||
FD_CLOSEONEXEC(ev_signal_pair[0]);
|
||||
FD_CLOSEONEXEC(ev_signal_pair[1]);
|
||||
@ -120,7 +113,7 @@ evsignal_add(sigset_t *evsigmask, struct event *ev)
|
||||
int evsignal;
|
||||
|
||||
if (ev->ev_events & (EV_READ|EV_WRITE))
|
||||
errx(1, "%s: EV_SIGNAL incompatible use", __func__);
|
||||
event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
|
||||
evsignal = EVENT_SIGNAL(ev);
|
||||
sigaddset(evsigmask, evsignal);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user