2 Unix SMB/CIFS implementation.
3 main select loop and event handling
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) Stefan Metzmacher 2009
7 ** NOTE! The following LGPL license applies to the tevent
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 PLEASE READ THIS BEFORE MODIFYING!
28 This module is a general abstraction for the main select loop and
29 event handling. Do not ever put any localised hacks in here, instead
30 register one of the possible event types and implement that event
33 There are 2 types of event handling that are handled in this module:
35 1) a file descriptor becoming readable or writeable. This is mostly
36 used for network sockets, but can be used for any type of file
37 descriptor. You may only register one handler for each file
38 descriptor/io combination or you will get unpredictable results
39 (this means that you can have a handler for read events, and a
40 separate handler for write events, but not two handlers that are
41 both handling read events)
43 2) a timed event. You can register an event that happens at a
44 specific time. You can register as many of these as you
45 like. They are single shot - add a new timed event in the event
46 handler to get another event.
48 To setup a set of events you first need to create a event_context
49 structure using the function tevent_context_init(); This returns a
50 'struct tevent_context' that you use in all subsequent calls.
52 After that you can add/remove events that you are interested in
53 using tevent_add_*() and talloc_free()
55 Finally, you call tevent_loop_wait_once() to block waiting for one of the
56 events to occor or tevent_loop_wait() which will loop
61 #include "system/filesys.h"
63 #include "system/threads.h"
65 #define TEVENT_DEPRECATED 1
67 #include "tevent_internal.h"
68 #include "tevent_util.h"
70 #include <sys/eventfd.h>
73 static void tevent_abort(struct tevent_context *ev, const char *reason);
75 struct tevent_ops_list {
76 struct tevent_ops_list *next, *prev;
78 const struct tevent_ops *ops;
81 /* list of registered event backends */
82 static struct tevent_ops_list *tevent_backends = NULL;
83 static char *tevent_default_backend = NULL;
86 register an events backend
88 bool tevent_register_backend(const char *name, const struct tevent_ops *ops)
90 struct tevent_ops_list *e;
92 for (e = tevent_backends; e != NULL; e = e->next) {
93 if (0 == strcmp(e->name, name)) {
94 /* already registered, skip it */
99 e = talloc(NULL, struct tevent_ops_list);
100 if (e == NULL) return false;
104 DLIST_ADD(tevent_backends, e);
110 set the default event backend
112 void tevent_set_default_backend(const char *backend)
114 talloc_free(tevent_default_backend);
115 tevent_default_backend = talloc_strdup(NULL, backend);
119 initialise backends if not already done
121 static void tevent_backend_init(void)
131 tevent_select_init();
133 tevent_poll_mt_init();
134 #if defined(HAVE_EPOLL)
136 #elif defined(HAVE_SOLARIS_PORTS)
140 tevent_kqueue_init();
142 tevent_standard_init();
145 _PRIVATE_ const struct tevent_ops *tevent_find_ops_byname(const char *name)
147 struct tevent_ops_list *e;
149 tevent_backend_init();
152 name = tevent_default_backend;
158 for (e = tevent_backends; e != NULL; e = e->next) {
159 if (0 == strcmp(e->name, name)) {
168 list available backends
170 const char **tevent_backend_list(TALLOC_CTX *mem_ctx)
172 const char **list = NULL;
173 struct tevent_ops_list *e;
175 tevent_backend_init();
177 for (e=tevent_backends;e;e=e->next) {
178 list = ev_str_list_add(list, e->name);
181 talloc_steal(mem_ctx, list);
186 static void tevent_common_wakeup_fini(struct tevent_context *ev);
190 static pthread_mutex_t tevent_contexts_mutex = PTHREAD_MUTEX_INITIALIZER;
191 static struct tevent_context *tevent_contexts = NULL;
192 static pthread_once_t tevent_atfork_initialized = PTHREAD_ONCE_INIT;
194 static void tevent_atfork_prepare(void)
196 struct tevent_context *ev;
199 ret = pthread_mutex_lock(&tevent_contexts_mutex);
204 for (ev = tevent_contexts; ev != NULL; ev = ev->next) {
205 ret = pthread_mutex_lock(&ev->scheduled_mutex);
207 tevent_abort(ev, "pthread_mutex_lock failed");
212 static void tevent_atfork_parent(void)
214 struct tevent_context *ev;
217 for (ev = DLIST_TAIL(tevent_contexts); ev != NULL;
218 ev = DLIST_PREV(ev)) {
219 ret = pthread_mutex_unlock(&ev->scheduled_mutex);
221 tevent_abort(ev, "pthread_mutex_unlock failed");
225 ret = pthread_mutex_unlock(&tevent_contexts_mutex);
231 static void tevent_atfork_child(void)
233 struct tevent_context *ev;
236 for (ev = DLIST_TAIL(tevent_contexts); ev != NULL;
237 ev = DLIST_PREV(ev)) {
238 struct tevent_threaded_context *tctx;
240 for (tctx = ev->threaded_contexts; tctx != NULL;
242 tctx->event_ctx = NULL;
245 ev->threaded_contexts = NULL;
247 ret = pthread_mutex_unlock(&ev->scheduled_mutex);
249 tevent_abort(ev, "pthread_mutex_unlock failed");
253 ret = pthread_mutex_unlock(&tevent_contexts_mutex);
259 static void tevent_prep_atfork(void)
263 ret = pthread_atfork(tevent_atfork_prepare,
264 tevent_atfork_parent,
265 tevent_atfork_child);
273 int tevent_common_context_destructor(struct tevent_context *ev)
275 struct tevent_fd *fd, *fn;
276 struct tevent_timer *te, *tn;
277 struct tevent_immediate *ie, *in;
278 struct tevent_signal *se, *sn;
283 ret = pthread_mutex_lock(&tevent_contexts_mutex);
288 DLIST_REMOVE(tevent_contexts, ev);
290 ret = pthread_mutex_unlock(&tevent_contexts_mutex);
296 if (ev->threaded_contexts != NULL) {
298 * Threaded contexts are indicators that threads are
299 * about to send us immediates via
300 * tevent_threaded_schedule_immediate. The caller
301 * needs to make sure that the tevent context lives
302 * long enough to receive immediates from all threads.
304 tevent_abort(ev, "threaded contexts exist");
307 tevent_common_wakeup_fini(ev);
309 for (fd = ev->fd_events; fd; fd = fn) {
311 fd->event_ctx = NULL;
312 DLIST_REMOVE(ev->fd_events, fd);
315 ev->last_zero_timer = NULL;
316 for (te = ev->timer_events; te; te = tn) {
318 te->event_ctx = NULL;
319 DLIST_REMOVE(ev->timer_events, te);
322 for (ie = ev->immediate_events; ie; ie = in) {
324 ie->event_ctx = NULL;
325 ie->cancel_fn = NULL;
326 DLIST_REMOVE(ev->immediate_events, ie);
329 for (se = ev->signal_events; se; se = sn) {
331 se->event_ctx = NULL;
332 DLIST_REMOVE(ev->signal_events, se);
334 * This is important, Otherwise signals
335 * are handled twice in child. eg, SIGHUP.
336 * one added in parent, and another one in
337 * the child. -- BoYang
339 tevent_cleanup_pending_signal_handlers(se);
342 /* removing nesting hook or we get an abort when nesting is
343 * not allowed. -- SSS
344 * Note that we need to leave the allowed flag at its current
345 * value, otherwise the use in tevent_re_initialise() will
346 * leave the event context with allowed forced to false, which
347 * will break users that expect nesting to be allowed
349 ev->nesting.level = 0;
350 ev->nesting.hook_fn = NULL;
351 ev->nesting.hook_private = NULL;
357 create a event_context structure for a specific implemementation.
358 This must be the first events call, and all subsequent calls pass
359 this event_context as the first element. Event handlers also
360 receive this as their first argument.
362 This function is for allowing third-party-applications to hook in gluecode
363 to their own event loop code, so that they can make async usage of our client libs
365 NOTE: use tevent_context_init() inside of samba!
367 struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
368 const struct tevent_ops *ops,
369 void *additional_data)
371 struct tevent_context *ev;
374 ev = talloc_zero(mem_ctx, struct tevent_context);
375 if (!ev) return NULL;
379 ret = pthread_once(&tevent_atfork_initialized, tevent_prep_atfork);
385 ret = pthread_mutex_init(&ev->scheduled_mutex, NULL);
391 ret = pthread_mutex_lock(&tevent_contexts_mutex);
393 pthread_mutex_destroy(&ev->scheduled_mutex);
398 DLIST_ADD(tevent_contexts, ev);
400 ret = pthread_mutex_unlock(&tevent_contexts_mutex);
407 talloc_set_destructor(ev, tevent_common_context_destructor);
410 ev->additional_data = additional_data;
412 ret = ev->ops->context_init(ev);
422 create a event_context structure. This must be the first events
423 call, and all subsequent calls pass this event_context as the first
424 element. Event handlers also receive this as their first argument.
426 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx,
429 const struct tevent_ops *ops;
431 ops = tevent_find_ops_byname(name);
436 return tevent_context_init_ops(mem_ctx, ops, NULL);
441 create a event_context structure. This must be the first events
442 call, and all subsequent calls pass this event_context as the first
443 element. Event handlers also receive this as their first argument.
445 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx)
447 return tevent_context_init_byname(mem_ctx, NULL);
452 return NULL on failure (memory allocation error)
454 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
458 tevent_fd_handler_t handler,
460 const char *handler_name,
461 const char *location)
463 return ev->ops->add_fd(ev, mem_ctx, fd, flags, handler, private_data,
464 handler_name, location);
468 set a close function on the fd event
470 void tevent_fd_set_close_fn(struct tevent_fd *fde,
471 tevent_fd_close_fn_t close_fn)
474 if (!fde->event_ctx) return;
475 fde->event_ctx->ops->set_fd_close_fn(fde, close_fn);
478 static void tevent_fd_auto_close_fn(struct tevent_context *ev,
479 struct tevent_fd *fde,
486 void tevent_fd_set_auto_close(struct tevent_fd *fde)
488 tevent_fd_set_close_fn(fde, tevent_fd_auto_close_fn);
492 return the fd event flags
494 uint16_t tevent_fd_get_flags(struct tevent_fd *fde)
497 if (!fde->event_ctx) return 0;
498 return fde->event_ctx->ops->get_fd_flags(fde);
502 set the fd event flags
504 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
507 if (!fde->event_ctx) return;
508 fde->event_ctx->ops->set_fd_flags(fde, flags);
511 bool tevent_signal_support(struct tevent_context *ev)
513 if (ev->ops->add_signal) {
519 static void (*tevent_abort_fn)(const char *reason);
521 void tevent_set_abort_fn(void (*abort_fn)(const char *reason))
523 tevent_abort_fn = abort_fn;
526 static void tevent_abort(struct tevent_context *ev, const char *reason)
528 tevent_debug(ev, TEVENT_DEBUG_FATAL,
529 "abort: %s\n", reason);
531 if (!tevent_abort_fn) {
535 tevent_abort_fn(reason);
540 return NULL on failure
542 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
544 struct timeval next_event,
545 tevent_timer_handler_t handler,
547 const char *handler_name,
548 const char *location)
550 return ev->ops->add_timer(ev, mem_ctx, next_event, handler, private_data,
551 handler_name, location);
555 allocate an immediate event
556 return NULL on failure (memory allocation error)
558 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
559 const char *location)
561 struct tevent_immediate *im;
563 im = talloc(mem_ctx, struct tevent_immediate);
564 if (im == NULL) return NULL;
568 im->event_ctx = NULL;
569 im->create_location = location;
571 im->private_data = NULL;
572 im->handler_name = NULL;
573 im->schedule_location = NULL;
574 im->cancel_fn = NULL;
575 im->additional_data = NULL;
581 schedule an immediate event
583 void _tevent_schedule_immediate(struct tevent_immediate *im,
584 struct tevent_context *ev,
585 tevent_immediate_handler_t handler,
587 const char *handler_name,
588 const char *location)
590 ev->ops->schedule_immediate(im, ev, handler, private_data,
591 handler_name, location);
597 sa_flags are flags to sigaction(2)
599 return NULL on failure
601 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
605 tevent_signal_handler_t handler,
607 const char *handler_name,
608 const char *location)
610 return ev->ops->add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data,
611 handler_name, location);
614 void tevent_loop_allow_nesting(struct tevent_context *ev)
616 ev->nesting.allowed = true;
619 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
620 tevent_nesting_hook hook,
623 if (ev->nesting.hook_fn &&
624 (ev->nesting.hook_fn != hook ||
625 ev->nesting.hook_private != private_data)) {
626 /* the way the nesting hook code is currently written
627 we cannot support two different nesting hooks at the
629 tevent_abort(ev, "tevent: Violation of nesting hook rules\n");
631 ev->nesting.hook_fn = hook;
632 ev->nesting.hook_private = private_data;
635 static void tevent_abort_nesting(struct tevent_context *ev, const char *location)
639 reason = talloc_asprintf(NULL, "tevent_loop_once() nesting at %s",
642 reason = "tevent_loop_once() nesting";
645 tevent_abort(ev, reason);
649 do a single event loop using the events defined in ev
651 int _tevent_loop_once(struct tevent_context *ev, const char *location)
654 void *nesting_stack_ptr = NULL;
658 if (ev->nesting.level > 1) {
659 if (!ev->nesting.allowed) {
660 tevent_abort_nesting(ev, location);
665 if (ev->nesting.level > 0) {
666 if (ev->nesting.hook_fn) {
668 ret2 = ev->nesting.hook_fn(ev,
669 ev->nesting.hook_private,
672 (void *)&nesting_stack_ptr,
681 tevent_trace_point_callback(ev, TEVENT_TRACE_BEFORE_LOOP_ONCE);
682 ret = ev->ops->loop_once(ev, location);
683 tevent_trace_point_callback(ev, TEVENT_TRACE_AFTER_LOOP_ONCE);
685 if (ev->nesting.level > 0) {
686 if (ev->nesting.hook_fn) {
688 ret2 = ev->nesting.hook_fn(ev,
689 ev->nesting.hook_private,
692 (void *)&nesting_stack_ptr,
707 this is a performance optimization for the samba4 nested event loop problems
709 int _tevent_loop_until(struct tevent_context *ev,
710 bool (*finished)(void *private_data),
712 const char *location)
715 void *nesting_stack_ptr = NULL;
719 if (ev->nesting.level > 1) {
720 if (!ev->nesting.allowed) {
721 tevent_abort_nesting(ev, location);
726 if (ev->nesting.level > 0) {
727 if (ev->nesting.hook_fn) {
729 ret2 = ev->nesting.hook_fn(ev,
730 ev->nesting.hook_private,
733 (void *)&nesting_stack_ptr,
742 while (!finished(private_data)) {
743 tevent_trace_point_callback(ev, TEVENT_TRACE_BEFORE_LOOP_ONCE);
744 ret = ev->ops->loop_once(ev, location);
745 tevent_trace_point_callback(ev, TEVENT_TRACE_AFTER_LOOP_ONCE);
751 if (ev->nesting.level > 0) {
752 if (ev->nesting.hook_fn) {
754 ret2 = ev->nesting.hook_fn(ev,
755 ev->nesting.hook_private,
758 (void *)&nesting_stack_ptr,
772 bool tevent_common_have_events(struct tevent_context *ev)
774 if (ev->fd_events != NULL) {
775 if (ev->fd_events != ev->wakeup_fde) {
778 if (ev->fd_events->next != NULL) {
783 * At this point we just have the wakeup pipe event as
784 * the only fd_event. That one does not count as a
785 * regular event, so look at the other event types.
789 return ((ev->timer_events != NULL) ||
790 (ev->immediate_events != NULL) ||
791 (ev->signal_events != NULL));
795 return on failure or (with 0) if all fd events are removed
797 int tevent_common_loop_wait(struct tevent_context *ev,
798 const char *location)
801 * loop as long as we have events pending
803 while (tevent_common_have_events(ev)) {
805 ret = _tevent_loop_once(ev, location);
807 tevent_debug(ev, TEVENT_DEBUG_FATAL,
808 "_tevent_loop_once() failed: %d - %s\n",
809 ret, strerror(errno));
814 tevent_debug(ev, TEVENT_DEBUG_WARNING,
815 "tevent_common_loop_wait() out of events\n");
820 return on failure or (with 0) if all fd events are removed
822 int _tevent_loop_wait(struct tevent_context *ev, const char *location)
824 return ev->ops->loop_wait(ev, location);
829 re-initialise a tevent context. This leaves you with the same
830 event context, but all events are wiped and the structure is
831 re-initialised. This is most useful after a fork()
833 zero is returned on success, non-zero on failure
835 int tevent_re_initialise(struct tevent_context *ev)
837 tevent_common_context_destructor(ev);
839 return ev->ops->context_init(ev);
842 static void wakeup_pipe_handler(struct tevent_context *ev,
843 struct tevent_fd *fde,
844 uint16_t flags, void *_private)
850 * This is the boilerplate for eventfd, but it works
851 * for pipes too. And as we don't care about the data
852 * we read, we're fine.
855 ret = read(fde->fd, &val, sizeof(val));
856 } while (ret == -1 && errno == EINTR);
860 * Initialize the wakeup pipe and pipe fde
863 int tevent_common_wakeup_init(struct tevent_context *ev)
867 if (ev->wakeup_fde != NULL) {
872 ret = eventfd(0, EFD_NONBLOCK);
880 ret = pipe(pipe_fds);
884 ev->wakeup_fd = pipe_fds[0];
885 ev->wakeup_write_fd = pipe_fds[1];
887 ev_set_blocking(ev->wakeup_fd, false);
888 ev_set_blocking(ev->wakeup_write_fd, false);
892 ev->wakeup_fde = tevent_add_fd(ev, ev, ev->wakeup_fd,
894 wakeup_pipe_handler, NULL);
895 if (ev->wakeup_fde == NULL) {
896 close(ev->wakeup_fd);
898 close(ev->wakeup_write_fd);
906 int tevent_common_wakeup(struct tevent_context *ev)
910 if (ev->wakeup_fde == NULL) {
917 ret = write(ev->wakeup_fd, &val, sizeof(val));
920 ret = write(ev->wakeup_write_fd, &c, 1);
922 } while ((ret == -1) && (errno == EINTR));
927 static void tevent_common_wakeup_fini(struct tevent_context *ev)
929 if (ev->wakeup_fde == NULL) {
933 TALLOC_FREE(ev->wakeup_fde);
935 close(ev->wakeup_fd);
937 close(ev->wakeup_write_fd);