TODO/UNTESTED: tevent: add kqueue backend
authorStefan Metzmacher <metze@samba.org>
Sun, 17 Feb 2013 14:03:03 +0000 (15:03 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 27 Jun 2016 19:42:02 +0000 (21:42 +0200)
lib/tevent/tevent.c
lib/tevent/tevent_internal.h
lib/tevent/tevent_kqueue.c [new file with mode: 0644]
lib/tevent/wscript

index 843cf0560f3b5190f4e1f10d9bd421e0db4cc047..4ab4c6cba36937db14a57982cf18186a59489905 100644 (file)
@@ -128,7 +128,9 @@ static void tevent_backend_init(void)
 #elif defined(HAVE_SOLARIS_PORTS)
        tevent_port_init();
 #endif
-
+#ifdef HAVE_KQUEUE
+       tevent_kqueue_init();
+#endif
        tevent_standard_init();
 }
 
index 10cc4a47f83e55b64ae3b6a247e79ed9137a2654..418981f50de931112ca3fae3f2343f401e138f85 100644 (file)
@@ -354,6 +354,9 @@ void tevent_epoll_set_panic_fallback(struct tevent_context *ev,
 #ifdef HAVE_SOLARIS_PORTS
 bool tevent_port_init(void);
 #endif
+#ifdef HAVE_KQUEUE
+bool tevent_kqueue_init(void);
+#endif
 
 
 void tevent_trace_point_callback(struct tevent_context *ev,
diff --git a/lib/tevent/tevent_kqueue.c b/lib/tevent/tevent_kqueue.c
new file mode 100644 (file)
index 0000000..98d2bc8
--- /dev/null
@@ -0,0 +1,499 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   main select loop and event handling - kqueue implementation
+
+   Copyright (C) Stefan Metzmacher 2013
+
+     ** NOTE! The following LGPL license applies to the tevent
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/time.h"
+#ifdef HAVE_SYS_EVENT_H
+#include <sys/event.h>
+#endif
+#include "tevent.h"
+#include "tevent_internal.h"
+#include "tevent_util.h"
+
+struct kqueue_event_context {
+       struct tevent_context *ev;
+       pid_t pid;
+       int kqueue_fd;
+};
+
+static void kqueue_panic(struct kqueue_event_context *kqueue_ev,
+                        const char *reason)
+{
+       struct tevent_context *ev = kqueue_ev->ev;
+
+       tevent_debug(ev, TEVENT_DEBUG_FATAL,
+                    "kqueue_panic: %s (%s) - calling abort()\n",
+                    reason, strerror(errno));
+       abort();
+}
+
+static int kqueue_ctx_destructor(struct kqueue_event_context *kqueue_ev)
+{
+       kqueue_ev->ev->additional_data = NULL;
+
+       if (kqueue_ev->kqueue_fd != -1) {
+               close(kqueue_ev->kqueue_fd);
+       }
+       kqueue_ev->kqueue_fd = -1;
+
+       return 0;
+}
+
+static int kqueue_event_context_init(struct tevent_context *ev)
+{
+       struct kqueue_event_context *kqueue_ev;
+
+       /*
+        * We might be called during tevent_re_initialise()
+        * which means we need to free our old additional_data.
+        */
+       TALLOC_FREE(ev->additional_data);
+
+       kqueue_ev = talloc_zero(ev, struct kqueue_event_context);
+       if (kqueue_ev == NULL) {
+               return -1;
+       }
+       kqueue_ev->ev = ev;
+       kqueue_ev->pid = getpid();
+       kqueue_ev->kqueue_fd = -1;
+
+       talloc_set_destructor(kqueue_ev, kqueue_ctx_destructor);
+
+       kqueue_ev->kqueue_fd = kqueue();
+       if (kqueue_ev->kqueue_fd == -1) {
+               TALLOC_FREE(kqueue_ev);
+               tevent_debug(ev, TEVENT_DEBUG_FATAL,
+                            "Failed to create kqueue() handle.\n");
+               return -1;
+       }
+
+       if (!ev_set_close_on_exec(kqueue_ev->kqueue_fd)) {
+               tevent_debug(kqueue_ev->ev, TEVENT_DEBUG_WARNING,
+                            "kqueue_fd pid[%d] - failed to set close-on-exec, "
+                            "file descriptor may be leaked to children.\n",
+                            kqueue_ev->pid);
+       }
+
+       ev->additional_data = kqueue_ev;
+       return 0;
+}
+
+static void kqueue_update_fd_event(struct kqueue_event_context *kqueue_ev,
+                                  struct tevent_fd *fde);
+
+static void kqueue_check_reopen(struct kqueue_event_context *kqueue_ev)
+{
+       struct tevent_fd *fde;
+
+       if (kqueue_ev->pid == getpid()) {
+               return;
+       }
+       kqueue_ev->pid = getpid();
+
+       close(kqueue_ev->kqueue_fd);
+       kqueue_ev->kqueue_fd = kqueue();
+       if (kqueue_ev->kqueue_fd == -1) {
+               kqueue_panic(kqueue_ev, "kqueue() failed");
+               return;
+       }
+
+       if (!ev_set_close_on_exec(kqueue_ev->kqueue_fd)) {
+               tevent_debug(kqueue_ev->ev, TEVENT_DEBUG_WARNING,
+                            "kqueue_fd pid[%d] - failed to set close-on-exec, "
+                            "file descriptor may be leaked to children.\n",
+                            kqueue_ev->pid);
+       }
+
+       for (fde=kqueue_ev->ev->fd_events;fde;fde=fde->next) {
+               fde->additional_flags = 0;
+               kqueue_update_fd_event(kqueue_ev, fde);
+       }
+}
+
+#define KQUEUE_ADDITIONAL_FD_FLAG_HAS_READ             (1<<0)
+#define KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_READ                (1<<1)
+#define KQUEUE_ADDITIONAL_FD_FLAG_HAS_WRITE            (1<<2)
+#define KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_WRITE       (1<<3)
+#define KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR         (1<<4)
+#define KQUEUE_ADDITIONAL_FD_FLAG_GOT_ERROR            (1<<5)
+
+static void kqueue_update_fd_event(struct kqueue_event_context *kqueue_ev,
+                                  struct tevent_fd *fde)
+{
+       bool add_read = false;
+       bool enable_read = false;
+       bool disable_read = false;
+       bool delete_read = false;
+       bool add_write = false;
+       bool enable_write = false;
+       bool disable_write = false;
+       bool delete_write = false;
+       bool got_error = (fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_GOT_ERROR);
+       struct kevent kev;
+       int ret;
+
+       if (fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_WRITE) {
+               if (got_error) {
+                       delete_write = true;
+               } else if (fde->flags & TEVENT_FD_WRITE) {
+                       enable_write = true;
+               } else if (fde->flags == 0) {
+                       delete_write = true;
+               }
+       } else if (fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_HAS_WRITE) {
+               if (got_error) {
+                       delete_write = true;
+               } else if (fde->flags == 0) {
+                       delete_write = true;
+               } else if (!(fde->flags & TEVENT_FD_WRITE)) {
+                       disable_write = true;
+               }
+       } else {
+               if (got_error) {
+                       /* nothing */
+               } else if (fde->flags & TEVENT_FD_WRITE) {
+                       add_write = true;
+               }
+       }
+
+       if (fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_READ) {
+               if (fde->flags & TEVENT_FD_READ) {
+                       enable_read = true;
+               } else if (fde->flags == 0) {
+                       delete_read = true;
+               }
+       } else if (fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_HAS_READ) {
+               if (fde->flags == 0) {
+                       delete_read = true;
+               } else if (!(fde->flags & TEVENT_FD_READ)) {
+                       disable_read = true;
+               }
+       } else {
+               if (fde->flags & TEVENT_FD_READ) {
+                       add_read = true;
+               }
+       }
+
+       if (add_write) {
+               EV_SET(&kev, fde->fd, EVFILT_WRITE, EV_ADD,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_WRITE EV_ADD failed");
+                       return;
+               }
+
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_HAS_WRITE;
+       } else if (enable_write) {
+               EV_SET(&kev, fde->fd, EVFILT_WRITE, EV_ENABLE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_WRITE EV_ENABLE failed");
+                       return;
+               }
+
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_WRITE;
+       } else if (disable_write) {
+               EV_SET(&kev, fde->fd, EVFILT_WRITE, EV_DISABLE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_WRITE EV_DISABLE failed");
+                       return;
+               }
+
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_WRITE;
+       } else if (delete_write) {
+               EV_SET(&kev, fde->fd, EVFILT_WRITE, EV_DELETE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_WRITE EV_DEL failed");
+                       return;
+               }
+
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_HAS_WRITE;
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_WRITE;
+       }
+
+       if (add_read) {
+               EV_SET(&kev, fde->fd, EVFILT_READ, EV_ADD,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_READ EV_ADD failed");
+                       return;
+               }
+
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_HAS_READ;
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR;
+       } else if (enable_read) {
+               EV_SET(&kev, fde->fd, EVFILT_READ, EV_ENABLE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_READ EV_ENABLE failed");
+                       return;
+               }
+
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_READ;
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR;
+       } else if (disable_read) {
+               EV_SET(&kev, fde->fd, EVFILT_READ, EV_DISABLE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_READ EV_DISABLE failed");
+                       return;
+               }
+
+               fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_READ;
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR;
+       } else if (delete_read) {
+               EV_SET(&kev, fde->fd, EVFILT_READ, EV_DELETE,
+                      0 /* fflags */, NULL /* data */,
+                      fde);
+               ret = kevent(kqueue_ev->kqueue_fd, &kev, 1, NULL, 0, NULL);
+               if (ret != 0) {
+                       kqueue_panic(kqueue_ev, "EVFILT_READ EV_DEL failed");
+                       return;
+               }
+
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_HAS_READ;
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_DISABLED_READ;
+               fde->additional_flags &= ~KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR;
+       }
+}
+
+/*
+  destroy an fd_event
+*/
+static int kqueue_event_fd_destructor(struct tevent_fd *fde)
+{
+       struct tevent_context *ev = fde->event_ctx;
+
+       if (ev) {
+               struct kqueue_event_context *kqueue_ev =
+                       talloc_get_type_abort(ev->additional_data,
+                       struct kqueue_event_context);
+               int flags = fde->flags;
+
+               kqueue_check_reopen(kqueue_ev);
+
+               fde->flags = 0;
+               kqueue_update_fd_event(kqueue_ev, fde);
+               fde->flags = flags;
+       }
+
+       return tevent_common_fd_destructor(fde);
+}
+
+static struct tevent_fd *kqueue_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
+                                           int fd, uint16_t flags,
+                                           tevent_fd_handler_t handler,
+                                           void *private_data,
+                                           const char *handler_name,
+                                           const char *location)
+{
+       struct kqueue_event_context *kqueue_ev =
+               talloc_get_type_abort(ev->additional_data,
+               struct kqueue_event_context);
+       struct tevent_fd *fde;
+
+       fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
+                                  handler, private_data,
+                                  handler_name, location);
+       if (fde == NULL) {
+               return NULL;
+       }
+       talloc_set_destructor(fde, kqueue_event_fd_destructor);
+
+       kqueue_check_reopen(kqueue_ev);
+
+       kqueue_update_fd_event(kqueue_ev, fde);
+
+       return fde;
+}
+
+/*
+  set the fd event flags
+*/
+static void kqueue_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
+{
+       struct tevent_context *ev;
+       struct kqueue_event_context *kqueue_ev;
+
+       if (fde->flags == flags) return;
+
+       ev = fde->event_ctx;
+       kqueue_ev = talloc_get_type_abort(ev->additional_data,
+                                         struct kqueue_event_context);
+
+       fde->flags = flags;
+
+       kqueue_check_reopen(kqueue_ev);
+       kqueue_update_fd_event(kqueue_ev, fde);
+}
+
+/*
+  event loop handling using kqueue
+*/
+static int kqueue_event_loop(struct kqueue_event_context *kqueue_ev, struct timeval *tvalp)
+{
+       int ret, i;
+#define MAXEVENTS 1
+       struct kevent kevs[MAXEVENTS];
+       struct timespec _ts;
+       const struct timespec *timeout = NULL;
+       int kevent_errno;
+
+       if (kqueue_ev->ev->signal_events &&
+           tevent_common_check_signal(kqueue_ev->ev)) {
+               return 0;
+       }
+
+       if (tvalp) {
+               _ts.tv_sec = tvalp->tv_sec;
+               _ts.tv_nsec = tvalp->tv_usec * 1000;
+               timeout = &_ts;
+       }
+
+       tevent_trace_point_callback(kqueue_ev->ev, TEVENT_TRACE_BEFORE_WAIT);
+       ret = kevent(kqueue_ev->kqueue_fd, NULL, 0, kevs, MAXEVENTS, timeout);
+       kevent_errno = errno;
+       tevent_trace_point_callback(kqueue_ev->ev, TEVENT_TRACE_AFTER_WAIT);
+
+       if (ret == -1 && kevent_errno == EINTR && kqueue_ev->ev->signal_events) {
+               if (tevent_common_check_signal(kqueue_ev->ev)) {
+                       return 0;
+               }
+       }
+
+       if (ret == -1 && kevent_errno != EINTR) {
+               kqueue_panic(kqueue_ev, "kevent() failed");
+               return -1;
+       }
+
+       if (ret == 0 && tvalp) {
+               /* we don't care about a possible delay here */
+               tevent_common_loop_timer_delay(kqueue_ev->ev);
+               return 0;
+       }
+
+       for (i=0;i<ret;i++) {
+               struct tevent_fd *fde = talloc_get_type(kevs[i].udata,
+                                                       struct tevent_fd);
+               uint16_t flags = 0;
+
+               if (fde == NULL) {
+                       kqueue_panic(kqueue_ev, "kevent() gave bad data");
+                       return -1;
+               }
+
+               if (kevs[i].flags & EV_EOF) {
+                       fde->additional_flags |= KQUEUE_ADDITIONAL_FD_FLAG_GOT_ERROR;
+                       /*
+                        * if we only wait for TEVENT_FD_WRITE, we should not
+                        * tell the event handler about it, and remove the
+                        * EVFILT_WRITE filter, as we only report errors when
+                        * waiting for read events, to match the select()
+                        * behavior
+                        */
+                       if (!(fde->additional_flags & KQUEUE_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
+                               kqueue_update_fd_event(kqueue_ev, fde);
+                               continue;
+                       }
+                       flags |= TEVENT_FD_READ;
+               }
+               if (kevs[i].filter == EVFILT_READ) {
+                       flags |= TEVENT_FD_READ;
+               }
+               if (kevs[i].filter == EVFILT_WRITE) {
+                       flags |= TEVENT_FD_WRITE;
+               }
+               if (flags) {
+                       fde->handler(kqueue_ev->ev, fde, flags, fde->private_data);
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+/*
+  do a single event loop using the events defined in ev
+*/
+static int kqueue_event_loop_once(struct tevent_context *ev, const char *location)
+{
+       struct kqueue_event_context *kqueue_ev = talloc_get_type(ev->additional_data,
+                                                          struct kqueue_event_context);
+       struct timeval tval;
+
+       if (ev->signal_events &&
+           tevent_common_check_signal(ev)) {
+               return 0;
+       }
+
+       if (ev->immediate_events &&
+           tevent_common_loop_immediate(ev)) {
+               return 0;
+       }
+
+       tval = tevent_common_loop_timer_delay(ev);
+       if (tevent_timeval_is_zero(&tval)) {
+               return 0;
+       }
+
+       kqueue_check_reopen(kqueue_ev);
+
+       return kqueue_event_loop(kqueue_ev, &tval);
+}
+
+static const struct tevent_ops kqueue_event_ops = {
+       .context_init           = kqueue_event_context_init,
+       .add_fd                 = kqueue_event_add_fd,
+       .set_fd_close_fn        = tevent_common_fd_set_close_fn,
+       .get_fd_flags           = tevent_common_fd_get_flags,
+       .set_fd_flags           = kqueue_event_set_fd_flags,
+       .add_timer              = tevent_common_add_timer,
+       .schedule_immediate     = tevent_common_schedule_immediate,
+       .add_signal             = tevent_common_add_signal,
+       .loop_once              = kqueue_event_loop_once,
+       .loop_wait              = tevent_common_loop_wait,
+};
+
+_PRIVATE_ bool tevent_kqueue_init(void)
+{
+       return tevent_register_backend("kqueue", &kqueue_event_ops);
+}
index 2bdb5ac80eba78dfd4ab3f791edba75dbacaa9d8..606438073c9314fb1768add649fb24c076a1de66 100755 (executable)
@@ -44,6 +44,9 @@ def configure(conf):
     if conf.CHECK_FUNCS('epoll_create', headers='sys/epoll.h'):
         conf.DEFINE('HAVE_EPOLL', 1)
 
+    if conf.CHECK_FUNCS('kqueue', headers='sys/types.h sys/event.h sys/time.h'):
+        conf.DEFINE('HAVE_KQUEUE', 1)
+
     tevent_num_signals = 64
     v = conf.CHECK_VALUEOF('NSIG', headers='signal.h')
     if v is not None:
@@ -91,6 +94,8 @@ def build(bld):
 
     if bld.CONFIG_SET('HAVE_SOLARIS_PORTS'):
         SRC += ' tevent_port.c'
+    if bld.CONFIG_SET('HAVE_KQUEUE'):
+        SRC += ' tevent_kqueue.c'
 
     if bld.env.standalone_tevent:
         bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'