2 * util/ub_event_pluggable.c - call registered pluggable event functions
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains an implementation for the indirection layer for pluggable
40 * events that calls the registered pluggable event loop. It also defines a
41 * default pluggable event loop based on the default libevent (compatibility)
46 #include "util/ub_event.h"
47 #include "libunbound/unbound-event.h"
48 #include "util/netevent.h"
50 #include "util/fptr_wlist.h"
52 /* We define libevent structures here to hide the libevent stuff. */
56 # include "util/winsock_event.h"
58 # include "util/mini_event.h"
59 # endif /* USE_WINSOCK */
60 #else /* USE_MINI_EVENT */
65 # include "event2/event.h"
66 # include "event2/event_struct.h"
67 # include "event2/event_compat.h"
69 #endif /* USE_MINI_EVENT */
71 #if UB_EV_TIMEOUT != EV_TIMEOUT || UB_EV_READ != EV_READ || \
72 UB_EV_WRITE != EV_WRITE || UB_EV_SIGNAL != EV_SIGNAL || \
73 UB_EV_PERSIST != EV_PERSIST
74 /* Only necessary for libev */
75 # define NATIVE_BITS(b) ( \
76 (((b) & UB_EV_TIMEOUT) ? EV_TIMEOUT : 0) \
77 | (((b) & UB_EV_READ ) ? EV_READ : 0) \
78 | (((b) & UB_EV_WRITE ) ? EV_WRITE : 0) \
79 | (((b) & UB_EV_SIGNAL ) ? EV_SIGNAL : 0) \
80 | (((b) & UB_EV_PERSIST) ? EV_PERSIST : 0))
82 # define UB_EV_BITS(b) ( \
83 (((b) & EV_TIMEOUT) ? UB_EV_TIMEOUT : 0) \
84 | (((b) & EV_READ ) ? UB_EV_READ : 0) \
85 | (((b) & EV_WRITE ) ? UB_EV_WRITE : 0) \
86 | (((b) & EV_SIGNAL ) ? UB_EV_SIGNAL : 0) \
87 | (((b) & EV_PERSIST) ? UB_EV_PERSIST : 0))
89 # define UB_EV_BITS_CB(C) void my_ ## C (int fd, short bits, void *arg) \
90 { (C)(fd, UB_EV_BITS(bits), arg); }
92 UB_EV_BITS_CB(comm_point_udp_callback);
93 UB_EV_BITS_CB(comm_point_udp_ancil_callback)
94 UB_EV_BITS_CB(comm_point_tcp_accept_callback)
95 UB_EV_BITS_CB(comm_point_tcp_handle_callback)
96 UB_EV_BITS_CB(comm_timer_callback)
97 UB_EV_BITS_CB(comm_signal_callback)
98 UB_EV_BITS_CB(comm_point_local_handle_callback)
99 UB_EV_BITS_CB(comm_point_raw_handle_callback)
100 UB_EV_BITS_CB(tube_handle_signal)
101 UB_EV_BITS_CB(comm_base_handle_slow_accept)
103 static void (*NATIVE_BITS_CB(void (*cb)(int, short, void*)))(int, short, void*)
105 if(cb == comm_point_udp_callback)
106 return my_comm_point_udp_callback;
107 else if(cb == comm_point_udp_ancil_callback)
108 return my_comm_point_udp_ancil_callback;
109 else if(cb == comm_point_tcp_accept_callback)
110 return my_comm_point_tcp_accept_callback;
111 else if(cb == comm_point_tcp_handle_callback)
112 return my_comm_point_tcp_handle_callback;
113 else if(cb == comm_timer_callback)
114 return my_comm_timer_callback;
115 else if(cb == comm_signal_callback)
116 return my_comm_signal_callback;
117 else if(cb == comm_point_local_handle_callback)
118 return my_comm_point_local_handle_callback;
119 else if(cb == comm_point_raw_handle_callback)
120 return my_comm_point_raw_handle_callback;
121 else if(cb == tube_handle_signal)
122 return my_tube_handle_signal;
123 else if(cb == comm_base_handle_slow_accept)
124 return my_comm_base_handle_slow_accept;
129 # define NATIVE_BITS(b) (b)
130 # define NATIVE_BITS_CB(c) (c)
134 #define EVFLAG_AUTO 0
137 struct my_event_base {
138 struct ub_event_base super;
139 struct event_base* base;
143 struct ub_event super;
147 #define AS_MY_EVENT_BASE(x) ((struct my_event_base*)x)
148 #define AS_MY_EVENT(x) ((struct my_event*)x)
150 const char* ub_event_get_version(void)
152 return "pluggable-event"PACKAGE_VERSION;
156 my_event_add_bits(struct ub_event* ev, short bits)
158 AS_MY_EVENT(ev)->ev.ev_events |= NATIVE_BITS(bits);
162 my_event_del_bits(struct ub_event* ev, short bits)
164 AS_MY_EVENT(ev)->ev.ev_events &= ~NATIVE_BITS(bits);
168 my_event_set_fd(struct ub_event* ev, int fd)
170 AS_MY_EVENT(ev)->ev.ev_fd = fd;
174 my_event_free(struct ub_event* ev)
176 free(AS_MY_EVENT(ev));
180 my_event_add(struct ub_event* ev, struct timeval* tv)
182 return event_add(&AS_MY_EVENT(ev)->ev, tv);
186 my_event_del(struct ub_event* ev)
188 return event_del(&AS_MY_EVENT(ev)->ev);
192 my_timer_add(struct ub_event* ev, struct ub_event_base* base,
193 void (*cb)(int, short, void*), void* arg, struct timeval* tv)
195 event_set(&AS_MY_EVENT(ev)->ev, -1, EV_TIMEOUT,NATIVE_BITS_CB(cb),arg);
196 if (event_base_set(AS_MY_EVENT_BASE(base)->base, &AS_MY_EVENT(ev)->ev)
199 return evtimer_add(&AS_MY_EVENT(ev)->ev, tv);
203 my_timer_del(struct ub_event* ev)
205 return evtimer_del(&AS_MY_EVENT(ev)->ev);
209 my_signal_add(struct ub_event* ev, struct timeval* tv)
211 return signal_add(&AS_MY_EVENT(ev)->ev, tv);
215 my_signal_del(struct ub_event* ev)
217 return signal_del(&AS_MY_EVENT(ev)->ev);
221 my_winsock_unregister_wsaevent(struct ub_event* ev)
223 #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
224 winsock_unregister_wsaevent(&AS_MY_EVENT(ev)->ev);
225 free(AS_MY_EVENT(ev));
232 my_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
234 #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
235 winsock_tcp_wouldblock(&AS_MY_EVENT(ev)->ev, NATIVE_BITS(eventbits));
242 static struct ub_event_vmt default_event_vmt = {
243 my_event_add_bits, my_event_del_bits, my_event_set_fd,
244 my_event_free, my_event_add, my_event_del,
245 my_timer_add, my_timer_del, my_signal_add, my_signal_del,
246 my_winsock_unregister_wsaevent, my_winsock_tcp_wouldblock
250 my_event_base_free(struct ub_event_base* base)
252 #ifdef USE_MINI_EVENT
253 event_base_free(AS_MY_EVENT_BASE(base)->base);
254 #elif defined(HAVE_EVENT_BASE_FREE) && defined(HAVE_EVENT_BASE_ONCE)
255 /* only libevent 1.2+ has it, but in 1.2 it is broken -
256 assertion fails on signal handling ev that is not deleted
257 in libevent 1.3c (event_base_once appears) this is fixed. */
258 event_base_free(AS_MY_EVENT_BASE(base)->base);
259 #endif /* HAVE_EVENT_BASE_FREE and HAVE_EVENT_BASE_ONCE */
260 free(AS_MY_EVENT_BASE(base));
264 my_event_base_dispatch(struct ub_event_base* base)
266 return event_base_dispatch(AS_MY_EVENT_BASE(base)->base);
270 my_event_base_loopexit(struct ub_event_base* base, struct timeval* tv)
272 return event_base_loopexit(AS_MY_EVENT_BASE(base)->base, tv);
275 static struct ub_event*
276 my_event_new(struct ub_event_base* base, int fd, short bits,
277 void (*cb)(int, short, void*), void* arg)
279 struct my_event *my_ev = (struct my_event*)calloc(1,
280 sizeof(struct my_event));
285 event_set(&my_ev->ev, fd, NATIVE_BITS(bits), NATIVE_BITS_CB(cb), arg);
286 if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
290 my_ev->super.magic = UB_EVENT_MAGIC;
291 my_ev->super.vmt = &default_event_vmt;
292 return &my_ev->super;
295 static struct ub_event*
296 my_signal_new(struct ub_event_base* base, int fd,
297 void (*cb)(int, short, void*), void* arg)
299 struct my_event *my_ev = (struct my_event*)calloc(1,
300 sizeof(struct my_event));
305 signal_set(&my_ev->ev, fd, NATIVE_BITS_CB(cb), arg);
306 if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
310 my_ev->super.magic = UB_EVENT_MAGIC;
311 my_ev->super.vmt = &default_event_vmt;
312 return &my_ev->super;
315 static struct ub_event*
316 my_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
317 void (*cb)(int, short, void*), void* arg)
319 #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
320 struct my_event *my_ev = (struct my_event*)calloc(1,
321 sizeof(struct my_event));
326 if (!winsock_register_wsaevent(AS_MY_EVENT_BASE(base)->base,
327 &my_ev->ev, wsaevent, cb, arg)) {
332 my_ev->super.magic = UB_EVENT_MAGIC;
333 my_ev->super.vmt = &default_event_vmt;
334 return &my_ev->super;
344 static struct ub_event_base_vmt default_event_base_vmt = {
345 my_event_base_free, my_event_base_dispatch,
346 my_event_base_loopexit, my_event_new, my_signal_new,
347 my_winsock_register_wsaevent
350 struct ub_event_base*
351 ub_default_event_base(int sigs, time_t* time_secs, struct timeval* time_tv)
353 struct my_event_base* my_base = (struct my_event_base*)calloc(1,
354 sizeof(struct my_event_base));
359 #ifdef USE_MINI_EVENT
361 /* use mini event time-sharing feature */
362 my_base->base = event_init(time_secs, time_tv);
366 # if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
369 my_base->base = (struct event_base*)ev_default_loop(EVFLAG_AUTO);
371 my_base->base = (struct event_base*)ev_loop_new(EVFLAG_AUTO);
374 # ifdef HAVE_EVENT_BASE_NEW
375 my_base->base = event_base_new();
377 my_base->base = event_init();
381 if (!my_base->base) {
385 my_base->super.magic = UB_EVENT_MAGIC;
386 my_base->super.vmt = &default_event_base_vmt;
387 return &my_base->super;
390 struct ub_event_base*
391 ub_libevent_event_base(struct event_base* base)
393 #ifdef USE_MINI_EVENT
397 struct my_event_base* my_base = (struct my_event_base*)calloc(1,
398 sizeof(struct my_event_base));
402 my_base->super.magic = UB_EVENT_MAGIC;
403 my_base->super.vmt = &default_event_base_vmt;
404 my_base->base = base;
405 return &my_base->super;
410 ub_libevent_get_event_base(struct ub_event_base* base)
412 #ifndef USE_MINI_EVENT
413 if (base->vmt == &default_event_base_vmt)
414 return AS_MY_EVENT_BASE(base)->base;
421 #if (defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)) && defined(EVBACKEND_SELECT)
422 static const char* ub_ev_backend2str_pluggable(int b)
425 case EVBACKEND_SELECT: return "select";
426 case EVBACKEND_POLL: return "poll";
427 case EVBACKEND_EPOLL: return "epoll";
428 case EVBACKEND_KQUEUE: return "kqueue";
429 case EVBACKEND_DEVPOLL: return "devpoll";
430 case EVBACKEND_PORT: return "evport";
437 ub_get_event_sys(struct ub_event_base* ub_base, const char** n, const char** s,
442 *n = "pluggable-event";
444 *m = "WSAWaitForMultipleEvents";
445 #elif defined(USE_MINI_EVENT)
447 *n = "pluggable-event";
451 struct event_base* b = ub_libevent_get_event_base(ub_base);
452 /* This function is only called from comm_base_create, so
453 * ub_base is guaranteed to exist and to be the default
457 *n = "pluggable-event";
458 *s = event_get_version();
459 # if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
460 *n = "pluggable-libev";
461 # ifdef EVBACKEND_SELECT
462 *m = ub_ev_backend2str_pluggable(ev_backend((struct ev_loop*)b));
464 *m = "not obtainable";
466 # elif defined(HAVE_EVENT_BASE_GET_METHOD)
467 *n = "pluggable-libevent";
468 *m = event_base_get_method(b);
470 *m = "not obtainable";
476 ub_event_base_free(struct ub_event_base* base)
478 if (base && base->magic == UB_EVENT_MAGIC) {
479 fptr_ok(base->vmt != &default_event_base_vmt ||
480 base->vmt->free == my_event_base_free);
481 (*base->vmt->free)(base);
486 ub_event_base_dispatch(struct ub_event_base* base)
488 if (base->magic == UB_EVENT_MAGIC) {
489 fptr_ok(base->vmt != &default_event_base_vmt ||
490 base->vmt->dispatch == my_event_base_dispatch);
491 return (*base->vmt->dispatch)(base);
497 ub_event_base_loopexit(struct ub_event_base* base)
499 if (base->magic == UB_EVENT_MAGIC) {
500 fptr_ok(base->vmt != &default_event_base_vmt ||
501 base->vmt->loopexit == my_event_base_loopexit);
502 return (*base->vmt->loopexit)(base, NULL);
508 ub_event_new(struct ub_event_base* base, int fd, short bits,
509 void (*cb)(int, short, void*), void* arg)
511 if (base->magic == UB_EVENT_MAGIC) {
512 fptr_ok(base->vmt != &default_event_base_vmt ||
513 base->vmt->new_event == my_event_new);
514 return (*base->vmt->new_event)(base, fd, bits, cb, arg);
520 ub_signal_new(struct ub_event_base* base, int fd,
521 void (*cb)(int, short, void*), void* arg)
523 if (base->magic == UB_EVENT_MAGIC) {
524 fptr_ok(base->vmt != &default_event_base_vmt ||
525 base->vmt->new_signal == my_signal_new);
526 return (*base->vmt->new_signal)(base, fd, cb, arg);
532 ub_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
533 void (*cb)(int, short, void*), void* arg)
535 if (base->magic == UB_EVENT_MAGIC) {
536 fptr_ok(base->vmt != &default_event_base_vmt ||
537 base->vmt->winsock_register_wsaevent ==
538 my_winsock_register_wsaevent);
539 return (*base->vmt->winsock_register_wsaevent)(base, wsaevent, cb, arg);
545 ub_event_add_bits(struct ub_event* ev, short bits)
547 if (ev->magic == UB_EVENT_MAGIC) {
548 fptr_ok(ev->vmt != &default_event_vmt ||
549 ev->vmt->add_bits == my_event_add_bits);
550 (*ev->vmt->add_bits)(ev, bits);
555 ub_event_del_bits(struct ub_event* ev, short bits)
557 if (ev->magic == UB_EVENT_MAGIC) {
558 fptr_ok(ev->vmt != &default_event_vmt ||
559 ev->vmt->del_bits == my_event_del_bits);
560 (*ev->vmt->del_bits)(ev, bits);
565 ub_event_set_fd(struct ub_event* ev, int fd)
567 if (ev->magic == UB_EVENT_MAGIC) {
568 fptr_ok(ev->vmt != &default_event_vmt ||
569 ev->vmt->set_fd == my_event_set_fd);
570 (*ev->vmt->set_fd)(ev, fd);
575 ub_event_free(struct ub_event* ev)
577 if (ev && ev->magic == UB_EVENT_MAGIC) {
578 fptr_ok(ev->vmt != &default_event_vmt ||
579 ev->vmt->free == my_event_free);
580 (*ev->vmt->free)(ev);
585 ub_event_add(struct ub_event* ev, struct timeval* tv)
587 if (ev->magic == UB_EVENT_MAGIC) {
588 fptr_ok(ev->vmt != &default_event_vmt ||
589 ev->vmt->add == my_event_add);
590 return (*ev->vmt->add)(ev, tv);
596 ub_event_del(struct ub_event* ev)
598 if (ev && ev->magic == UB_EVENT_MAGIC) {
599 fptr_ok(ev->vmt != &default_event_vmt ||
600 ev->vmt->del == my_event_del);
601 return (*ev->vmt->del)(ev);
607 ub_timer_add(struct ub_event* ev, struct ub_event_base* base,
608 void (*cb)(int, short, void*), void* arg, struct timeval* tv)
610 if (ev->magic == UB_EVENT_MAGIC) {
611 fptr_ok(ev->vmt != &default_event_vmt ||
612 ev->vmt->add_timer == my_timer_add);
613 return (*ev->vmt->add_timer)(ev, base, cb, arg, tv);
619 ub_timer_del(struct ub_event* ev)
621 if (ev && ev->magic == UB_EVENT_MAGIC) {
622 fptr_ok(ev->vmt != &default_event_vmt ||
623 ev->vmt->del_timer == my_timer_del);
624 return (*ev->vmt->del_timer)(ev);
630 ub_signal_add(struct ub_event* ev, struct timeval* tv)
632 if (ev->magic == UB_EVENT_MAGIC) {
633 fptr_ok(ev->vmt != &default_event_vmt ||
634 ev->vmt->add_signal == my_signal_add);
635 return (*ev->vmt->add_signal)(ev, tv);
641 ub_signal_del(struct ub_event* ev)
643 if (ev && ev->magic == UB_EVENT_MAGIC) {
644 fptr_ok(ev->vmt != &default_event_vmt ||
645 ev->vmt->del_signal == my_signal_del);
646 return (*ev->vmt->del_signal)(ev);
652 ub_winsock_unregister_wsaevent(struct ub_event* ev)
654 if (ev && ev->magic == UB_EVENT_MAGIC) {
655 fptr_ok(ev->vmt != &default_event_vmt ||
656 ev->vmt->winsock_unregister_wsaevent ==
657 my_winsock_unregister_wsaevent);
658 (*ev->vmt->winsock_unregister_wsaevent)(ev);
663 ub_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
665 if (ev->magic == UB_EVENT_MAGIC) {
666 fptr_ok(ev->vmt != &default_event_vmt ||
667 ev->vmt->winsock_tcp_wouldblock ==
668 my_winsock_tcp_wouldblock);
669 (*ev->vmt->winsock_tcp_wouldblock)(ev, eventbits);
673 void ub_comm_base_now(struct comm_base* cb)
678 #ifdef USE_MINI_EVENT
679 /** minievent updates the time when it blocks. */
680 if (comm_base_internal(cb)->magic == UB_EVENT_MAGIC &&
681 comm_base_internal(cb)->vmt == &default_event_base_vmt)
682 return; /* Actually using mini event, so do not set time */
683 #endif /* USE_MINI_EVENT */
685 /** fillup the time values in the event base */
686 comm_base_timept(cb, &tt, &tv);
687 if(gettimeofday(tv, NULL) < 0) {
688 log_err("gettimeofday: %s", strerror(errno));