[PATCH] Fix tevent standard to fallback to poll instead of select.

Jeremy Allison jra at samba.org
Wed Jan 30 12:42:20 MST 2013


Since Volker's patchset 4f9cffbae6a60268140eba5e457ac7e86cac6246..15596a8d9c5a578fa98e110350d15265c90e8f03
in master winbindd now uses the tevent "standard" backend instead of explicitly
using the poll backend.

This massively improves scalability on Linux, but causes an issue
on non-Linux platforms. This is because the current tevent "standard"
backend is a mixture of cut-and-paste of the epoll backend mixed with
a runtime fallback to select() for plafroms that don't have epoll.

Attached is a patchset I've tested thoroughly (on Linux at least :-)
that removes the cut-and-pasted epoll/select based code and replaces
it with a new clean "standard" backend that wraps the existing epoll
and poll tevent backend modules, and safely falls back from epoll
to poll if epoll doesn't exist on the compiling platform, or if
epoll exists but has a runtime error.

Ira, I'd be very interested to hear reports of testing on
Solaris, and Richard, similarly for testing on FreeBSD.

Please review and push to master if you're happy with it !

Cheers,

	Jeremy.
-------------- next part --------------
>From 76d642043c69ae4927de57fb6ea6b2c7f235976d Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 10:32:56 -0800
Subject: [PATCH 01/10] Preparing to fix "standard" backend fallback.
 Initialize standard after epoll.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c
index fa842e4..e307dba 100644
--- a/lib/tevent/tevent.c
+++ b/lib/tevent/tevent.c
@@ -115,10 +115,10 @@ static void tevent_backend_init(void)
 	tevent_select_init();
 	tevent_poll_init();
 	tevent_poll_mt_init();
-	tevent_standard_init();
 #ifdef HAVE_EPOLL
 	tevent_epoll_init();
 #endif
+	tevent_standard_init();
 }
 
 /*
-- 
1.7.10.4


>From d36a07e0e9fed0415e3c3f77cc7a556a744685ab Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:15:02 -0800
Subject: [PATCH 02/10] Ensure we return after every call to epoll_panic().

Currently we can't return from this, but the new fallback
code will change this.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_epoll.c |    2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c
index 5f93de2..8e7bc4d 100644
--- a/lib/tevent/tevent_epoll.c
+++ b/lib/tevent/tevent_epoll.c
@@ -152,6 +152,7 @@ static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_
 	event.data.ptr = fde;
 	if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
 		epoll_panic(epoll_ev, "EPOLL_CTL_ADD failed");
+		return;
 	}
 	fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
 
@@ -201,6 +202,7 @@ static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_
 	event.data.ptr = fde;
 	if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
 		epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed");
+		return;
 	}
 
 	/* only if we want to read we want to tell the event handler about errors */
-- 
1.7.10.4


>From accfc07118ac47e3d7b664421c708e48abf43221 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:18:02 -0800
Subject: [PATCH 03/10] Add an internal function epoll_set_panic_fallback().

Can be set externally, allows us to fallback if epoll
fails at runtime.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_epoll.c    |   23 +++++++++++++++++++++++
 lib/tevent/tevent_internal.h |    2 ++
 2 files changed, 25 insertions(+)

diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c
index 8e7bc4d..a58124f 100644
--- a/lib/tevent/tevent_epoll.c
+++ b/lib/tevent/tevent_epoll.c
@@ -39,9 +39,32 @@ struct epoll_event_context {
 	int epoll_fd;
 
 	pid_t pid;
+
+	bool (*panic_fallback)(struct tevent_context *);
 };
 
 /*
+  called to set the panic fallback function.
+*/
+_PRIVATE_ bool epoll_set_panic_fallback(struct tevent_context *ev,
+				bool (*panic_fallback)(struct tevent_context *))
+{
+	struct epoll_event_context *epoll_ev;
+
+	if (ev->additional_data == NULL) {
+		return false;
+	}
+
+	epoll_ev = talloc_get_type(ev->additional_data,
+					struct epoll_event_context);
+	if (epoll_ev == NULL) {
+		return false;
+	}
+	epoll_ev->panic_fallback = panic_fallback;
+	return true;
+}
+
+/*
   called when a epoll call fails
 */
 static void epoll_panic(struct epoll_event_context *epoll_ev, const char *reason)
diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h
index f09cf57..d979f3a 100644
--- a/lib/tevent/tevent_internal.h
+++ b/lib/tevent/tevent_internal.h
@@ -318,6 +318,8 @@ bool tevent_poll_init(void);
 bool tevent_poll_mt_init(void);
 #ifdef HAVE_EPOLL
 bool tevent_epoll_init(void);
+bool epoll_set_panic_fallback(struct tevent_context *ev,
+			bool (*panic_fallback)(struct tevent_context *));
 #endif
 
 void tevent_trace_point_callback(struct tevent_context *ev,
-- 
1.7.10.4


>From 3a9a10fadcc0e43f3b5c6ed59f051a707770eea4 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:19:42 -0800
Subject: [PATCH 04/10] Plumb in the panic fallback code into the
 epoll_panic() runtime call.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_epoll.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c
index a58124f..d4d19d2 100644
--- a/lib/tevent/tevent_epoll.c
+++ b/lib/tevent/tevent_epoll.c
@@ -69,9 +69,20 @@ _PRIVATE_ bool epoll_set_panic_fallback(struct tevent_context *ev,
 */
 static void epoll_panic(struct epoll_event_context *epoll_ev, const char *reason)
 {
-	tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
-		 "%s (%s) - calling abort()\n", reason, strerror(errno));
-	abort();
+	if (epoll_ev->panic_fallback) {
+		if (!epoll_ev->panic_fallback(epoll_ev->ev)) {
+			/* Fallback failed. */
+			tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
+				"%s (%s) - calling abort()\n",
+				reason, strerror(errno));
+			abort();
+		}
+		talloc_free(epoll_ev);
+	} else {
+		tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
+			"%s (%s) - calling abort()\n", reason, strerror(errno));
+		abort();
+	}
 }
 
 /*
-- 
1.7.10.4


>From 810e0adf51210b5726370cc300978974a2420cd8 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:27:07 -0800
Subject: [PATCH 05/10] Add in some test code to allow the panic fallback path
 to be tested.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_epoll.c |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c
index d4d19d2..25b2dd3 100644
--- a/lib/tevent/tevent_epoll.c
+++ b/lib/tevent/tevent_epoll.c
@@ -43,6 +43,24 @@ struct epoll_event_context {
 	bool (*panic_fallback)(struct tevent_context *);
 };
 
+#ifdef TEST_PANIC_FALLBACK
+static int epoll_wait_panic_fallback(int epfd,
+				struct epoll_event *events,
+				int maxevents,
+				int timeout)
+{
+	/* 50% of the time, fail... */
+	if ((random() % 2) == 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return epoll_wait(epfd, events, maxevents, timeout);
+}
+
+#define epoll_wait epoll_wait_panic_fallback
+#endif
+
 /*
   called to set the panic fallback function.
 */
-- 
1.7.10.4


>From 780f795ac580c77f0654f727233c280436c9bca3 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:29:12 -0800
Subject: [PATCH 06/10] Add a private function into
 poll_event_add_fd_internal().

Not yet used, but will be called by the "standard"
fallback from epoll -> poll backends.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_internal.h |    4 ++++
 lib/tevent/tevent_poll.c     |   11 +++++++++++
 2 files changed, 15 insertions(+)

diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h
index d979f3a..a57a3c3 100644
--- a/lib/tevent/tevent_internal.h
+++ b/lib/tevent/tevent_internal.h
@@ -322,5 +322,9 @@ bool epoll_set_panic_fallback(struct tevent_context *ev,
 			bool (*panic_fallback)(struct tevent_context *));
 #endif
 
+void poll_event_add_fd_internal(struct tevent_context *ev,
+				struct tevent_fd *fde);
+
+
 void tevent_trace_point_callback(struct tevent_context *ev,
 				 enum tevent_trace_point);
diff --git a/lib/tevent/tevent_poll.c b/lib/tevent/tevent_poll.c
index da8cc01..a4b4ae3 100644
--- a/lib/tevent/tevent_poll.c
+++ b/lib/tevent/tevent_poll.c
@@ -224,6 +224,17 @@ static void poll_event_schedule_immediate(struct tevent_immediate *im,
 	poll_event_wake_pollthread(poll_ev);
 }
 
+_PRIVATE_ void poll_event_add_fd_internal(struct tevent_context *ev,
+					struct tevent_fd *fde)
+{
+	struct poll_event_context *poll_ev = talloc_get_type_abort(
+		ev->additional_data, struct poll_event_context);
+
+	DLIST_ADD(poll_ev->fresh, fde);
+	talloc_set_destructor(fde, poll_fresh_fde_destructor);
+	poll_event_wake_pollthread(poll_ev);
+}
+
 /*
   add a fd based event
   return NULL on failure (memory allocation error)
-- 
1.7.10.4


>From 5dacae13d5901d0f97b2287326f0592a72723d24 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:31:12 -0800
Subject: [PATCH 07/10] Fix poll_event_add_fd() to use the common
 tevent_common_add_fd() backend and then fix up the
 struct poll_event_context struct.

Makes more code common between backends.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_poll.c |   29 +++++++----------------------
 1 file changed, 7 insertions(+), 22 deletions(-)

diff --git a/lib/tevent/tevent_poll.c b/lib/tevent/tevent_poll.c
index a4b4ae3..76c3d6d 100644
--- a/lib/tevent/tevent_poll.c
+++ b/lib/tevent/tevent_poll.c
@@ -247,32 +247,17 @@ static struct tevent_fd *poll_event_add_fd(struct tevent_context *ev,
 					   const char *handler_name,
 					   const char *location)
 {
-	struct poll_event_context *poll_ev = talloc_get_type_abort(
-		ev->additional_data, struct poll_event_context);
-	struct tevent_fd *fde;
-
-	if (fd < 0) {
+	struct tevent_fd *fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
+					handler, private_data,
+					handler_name, location);
+	if (!fde) {
 		return NULL;
 	}
 
-	fde = talloc(mem_ctx ? mem_ctx : ev, struct tevent_fd);
-	if (fde == NULL) {
-		return NULL;
-	}
-	fde->event_ctx		= ev;
-	fde->fd			= fd;
-	fde->flags		= flags;
-	fde->handler		= handler;
-	fde->close_fn		= NULL;
-	fde->private_data	= private_data;
-	fde->handler_name	= handler_name;
-	fde->location		= location;
-	fde->additional_flags	= 0;
-	fde->additional_data	= NULL;
+	/* Remove from the ev->fd_events list. */
+	DLIST_REMOVE(ev->fd_events, fde);
 
-	DLIST_ADD(poll_ev->fresh, fde);
-	talloc_set_destructor(fde, poll_fresh_fde_destructor);
-	poll_event_wake_pollthread(poll_ev);
+	poll_event_add_fd_internal(ev, fde);
 
 	/*
 	 * poll_event_loop_poll will take care of the rest in
-- 
1.7.10.4


>From b621369fd7091fb71bf9d8af880f962f925a22f8 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 10:34:31 -0800
Subject: [PATCH 08/10] Add a utility function tevent_find_ops_byname().

Returns an event ops struct given a string name. Not
yet used, but will be part of the new "standard" fallback
code.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent.c          |   13 +++++++++++++
 lib/tevent/tevent_internal.h |    1 +
 2 files changed, 14 insertions(+)

diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c
index e307dba..8376053 100644
--- a/lib/tevent/tevent.c
+++ b/lib/tevent/tevent.c
@@ -74,6 +74,19 @@ struct tevent_ops_list {
 static struct tevent_ops_list *tevent_backends = NULL;
 static char *tevent_default_backend = NULL;
 
+_PRIVATE_ const struct tevent_ops *tevent_find_ops_byname(const char *name)
+{
+	struct tevent_ops_list *e;
+
+	for (e = tevent_backends; e != NULL; e = e->next) {
+		if (0 == strcmp(e->name, name)) {
+			return e->ops;
+		}
+	}
+
+	return NULL;
+}
+
 /*
   register an events backend
 */
diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h
index a57a3c3..45b4e4c 100644
--- a/lib/tevent/tevent_internal.h
+++ b/lib/tevent/tevent_internal.h
@@ -265,6 +265,7 @@ struct tevent_context {
 	} tracing;
 };
 
+const struct tevent_ops *tevent_find_ops_byname(const char *name);
 
 int tevent_common_context_destructor(struct tevent_context *ev);
 int tevent_common_loop_wait(struct tevent_context *ev,
-- 
1.7.10.4


>From 573a39d954e0c88762f8771d4b322404487e9c1a Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:38:39 -0800
Subject: [PATCH 09/10] Add in the new implementation of "standard" tevent
 backend.

Falls back cleanly from epoll -> poll, or uses poll if
epoll not available.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_standard.c |  147 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 143 insertions(+), 4 deletions(-)

diff --git a/lib/tevent/tevent_standard.c b/lib/tevent/tevent_standard.c
index 1e33720..8051d20 100644
--- a/lib/tevent/tevent_standard.c
+++ b/lib/tevent/tevent_standard.c
@@ -3,6 +3,7 @@
    main select loop and event handling
    Copyright (C) Andrew Tridgell	2003-2005
    Copyright (C) Stefan Metzmacher	2005-2009
+   Copyright (C) Jeremy Allison         2013
 
      ** NOTE! The following LGPL license applies to the tevent
      ** library. This does NOT imply that all of Samba is released
@@ -26,18 +27,18 @@
   This is SAMBA's default event loop code
 
   - we try to use epoll if configure detected support for it
-    otherwise we use select()
+    otherwise we use poll()
   - if epoll is broken on the system or the kernel doesn't support it
-    at runtime we fallback to select()
+    at runtime we fallback to poll()
 */
 
 #include "replace.h"
 #include "system/filesys.h"
-#include "system/select.h"
 #include "tevent.h"
 #include "tevent_util.h"
 #include "tevent_internal.h"
 
+#if 0
 struct std_event_context {
 	/* a pointer back to the generic event_context */
 	struct tevent_context *ev;
@@ -583,10 +584,148 @@ static const struct tevent_ops std_event_ops = {
 	.loop_once		= std_event_loop_once,
 	.loop_wait		= tevent_common_loop_wait,
 };
+#endif
+
+/*
+  If this function gets called. epoll failed at runtime.
+  Move us to using poll instead. If we return false here,
+  caller should abort().
+*/
+static bool std_fallback_to_poll(struct tevent_context *ev)
+{
+	int ret;
+	struct tevent_fd *fde;
+	struct tevent_fd *fde_next;
+	const struct tevent_ops *poll_ops = tevent_find_ops_byname("poll");
+
+	/* First switch all the ops to poll. */
+	ev->ops = poll_ops;
+
+	/* Next initialize the poll backend. */
+	ret = ev->ops->context_init(ev);
+	if (ret != 0) {
+		return false;
+	}
+
+	/*
+	 * Now we have to change all the existing file descriptor
+	 * events from the epoll backend to the poll backend.
+	 */
+	for (fde = ev->fd_events; fde; fde = fde_next) {
+		/*
+		 * poll_event_add_fd_internal() moves
+		 * the fde off the ev->fd_events list.
+		 */
+		fde_next = fde->next;
+
+		/* Remove from the ev->fd_events list. */
+		DLIST_REMOVE(ev->fd_events, fde);
+
+		/* Re-add this event as a poll backend event. */
+		poll_event_add_fd_internal(ev, fde);
+	}
+
+	return true;
+}
+
+static int std_event_loop_once(struct tevent_context *ev, const char *location)
+{
+	const struct tevent_ops *epoll_ops =
+			*talloc_get_type(talloc_parent(ev->ops),
+					const struct tevent_ops *);
+	int ret;
+
+	ret = epoll_ops->loop_once(ev, location);
+	if (ret == -1) {
+		/* Did we fallback ? */
+		if (ev->ops == tevent_find_ops_byname("poll")) {
+			/* Yes. Retry with poll. */
+			ret = ev->ops->loop_once(ev, location);
+		}
+	}
+
+	return ret;
+}
+
+/*
+  Initialize the epoll backend and allow it to call a
+  switch function if epoll fails at runtime.
+*/
+static int std_event_context_init(struct tevent_context *ev)
+{
+	const struct tevent_ops *poll_ops = tevent_find_ops_byname("poll");
+	const struct tevent_ops *epoll_ops = tevent_find_ops_byname("epoll");
+	const struct tevent_ops *std_ops = tevent_find_ops_byname("standard");
+
+	if (epoll_ops == NULL) {
+		/*
+		 * No epoll backend. Make "standard"
+		 * the same as "poll".
+		 */
+		ev->ops = poll_ops;
+		return ev->ops->context_init(ev);
+	}
 
+	/*
+	 * If this is the first initialization
+	 * we need to set up the allocated ops
+	 * pointers.
+	 */
+
+	if (ev->ops == std_ops) {
+		struct tevent_ops *custom_ops;
+		const struct tevent_ops **pp_ops;
+
+		/*
+		 * Save off the epoll_ops pointer
+		 * so we can get to epoll_ops->loop_once
+		 * inside our custom loop_once.
+		 */
+		pp_ops = talloc_zero(ev, const struct tevent_ops *);
+		if (pp_ops == NULL) {
+			return -1;
+		}
+
+		*pp_ops = epoll_ops;
+
+		/* Allocate space for our custom ops. */
+		custom_ops = talloc_zero(pp_ops, struct tevent_ops);
+		if (custom_ops == NULL) {
+			talloc_free(pp_ops);
+			return -1;
+		}
+
+		/*
+		 * Set custom_ops the same as epoll,
+		 * except re-init using std_event_context_init()
+		 * and use std_event_loop_once() to add the
+		 * ability to fallback to a poll backend on
+		 * epoll runtime error.
+		 */
+		memcpy(custom_ops, epoll_ops, sizeof(struct tevent_ops));
+		custom_ops->context_init = std_ops->context_init;
+		custom_ops->loop_once = std_ops->loop_once;
+
+		ev->ops = custom_ops;
+	}
+
+	if (epoll_ops->context_init(ev) == -1) {
+		return -1;
+	}
+#ifdef HAVE_EPOLL
+	if (!epoll_set_panic_fallback(ev, std_fallback_to_poll)) {
+		return -1;
+	}
+#endif
+	return 0;
+}
+
+static const struct tevent_ops std_event_ops = {
+	.context_init           = std_event_context_init,
+	.loop_once		= std_event_loop_once,
+};
 
 _PRIVATE_ bool tevent_standard_init(void)
 {
 	return tevent_register_backend("standard", &std_event_ops);
 }
-
-- 
1.7.10.4


>From fe17d5d6dcce2bb7e6bdb40bc2bb79aa02714bea Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Tue, 29 Jan 2013 11:40:09 -0800
Subject: [PATCH 10/10] Remove the previous "standard" tevent backend
 implementation.

This was a horrible hybrid of duplicated epoll and select()
code.

Signed-off-by: Jeremy Allison <jra at samba.org>
---
 lib/tevent/tevent_standard.c |  548 ------------------------------------------
 1 file changed, 548 deletions(-)

diff --git a/lib/tevent/tevent_standard.c b/lib/tevent/tevent_standard.c
index 8051d20..d7ec3dd 100644
--- a/lib/tevent/tevent_standard.c
+++ b/lib/tevent/tevent_standard.c
@@ -38,554 +38,6 @@
 #include "tevent_util.h"
 #include "tevent_internal.h"
 
-#if 0
-struct std_event_context {
-	/* a pointer back to the generic event_context */
-	struct tevent_context *ev;
-
-	/* the maximum file descriptor number in fd_events */
-	int maxfd;
-
-	/* information for exiting from the event loop */
-	int exit_code;
-
-	/* when using epoll this is the handle from epoll_create */
-	int epoll_fd;
-
-	/* our pid at the time the epoll_fd was created */
-	pid_t pid;
-};
-
-/* use epoll if it is available */
-#if HAVE_EPOLL
-/*
-  called when a epoll call fails, and we should fallback
-  to using select
-*/
-static void epoll_fallback_to_select(struct std_event_context *std_ev, const char *reason)
-{
-	tevent_debug(std_ev->ev, TEVENT_DEBUG_FATAL,
-		     "%s (%s) - falling back to select()\n",
-		     reason, strerror(errno));
-	close(std_ev->epoll_fd);
-	std_ev->epoll_fd = -1;
-	talloc_set_destructor(std_ev, NULL);
-}
-
-/*
-  map from TEVENT_FD_* to EPOLLIN/EPOLLOUT
-*/
-static uint32_t epoll_map_flags(uint16_t flags)
-{
-	uint32_t ret = 0;
-	if (flags & TEVENT_FD_READ) ret |= (EPOLLIN | EPOLLERR | EPOLLHUP);
-	if (flags & TEVENT_FD_WRITE) ret |= (EPOLLOUT | EPOLLERR | EPOLLHUP);
-	return ret;
-}
-
-/*
- free the epoll fd
-*/
-static int epoll_ctx_destructor(struct std_event_context *std_ev)
-{
-	if (std_ev->epoll_fd != -1) {
-		close(std_ev->epoll_fd);
-	}
-	std_ev->epoll_fd = -1;
-	return 0;
-}
-
-/*
- init the epoll fd
-*/
-static void epoll_init_ctx(struct std_event_context *std_ev)
-{
-	std_ev->epoll_fd = epoll_create(64);
-	if (std_ev->epoll_fd == -1) {
-		tevent_debug(std_ev->ev, TEVENT_DEBUG_FATAL,
-			     "Failed to create epoll handle.\n");
-		return;
-	}
-
-	if (!ev_set_close_on_exec(std_ev->epoll_fd)) {
-		tevent_debug(std_ev->ev, TEVENT_DEBUG_WARNING,
-			     "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
-	}
-
-	std_ev->pid = getpid();
-	talloc_set_destructor(std_ev, epoll_ctx_destructor);
-}
-
-static void epoll_add_event(struct std_event_context *std_ev, struct tevent_fd *fde);
-
-/*
-  reopen the epoll handle when our pid changes
-  see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an 
-  demonstration of why this is needed
- */
-static void epoll_check_reopen(struct std_event_context *std_ev)
-{
-	struct tevent_fd *fde;
-
-	if (std_ev->pid == getpid()) {
-		return;
-	}
-
-	close(std_ev->epoll_fd);
-	std_ev->epoll_fd = epoll_create(64);
-	if (std_ev->epoll_fd == -1) {
-		tevent_debug(std_ev->ev, TEVENT_DEBUG_FATAL,
-			     "Failed to recreate epoll handle after fork\n");
-		return;
-	}
-
-	if (!ev_set_close_on_exec(std_ev->epoll_fd)) {
-		tevent_debug(std_ev->ev, TEVENT_DEBUG_WARNING,
-			     "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
-	}
-
-	std_ev->pid = getpid();
-	for (fde=std_ev->ev->fd_events;fde;fde=fde->next) {
-		epoll_add_event(std_ev, fde);
-	}
-}
-
-#define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT	(1<<0)
-#define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR	(1<<1)
-#define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR	(1<<2)
-
-/*
- add the epoll event to the given fd_event
-*/
-static void epoll_add_event(struct std_event_context *std_ev, struct tevent_fd *fde)
-{
-	struct epoll_event event;
-	if (std_ev->epoll_fd == -1) return;
-
-	fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-
-	/* if we don't want events yet, don't add an epoll_event */
-	if (fde->flags == 0) return;
-
-	ZERO_STRUCT(event);
-	event.events = epoll_map_flags(fde->flags);
-	event.data.ptr = fde;
-	if (epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
-		epoll_fallback_to_select(std_ev, "EPOLL_CTL_ADD failed");
-	}
-	fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
-
-	/* only if we want to read we want to tell the event handler about errors */
-	if (fde->flags & TEVENT_FD_READ) {
-		fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-	}
-}
-
-/*
- delete the epoll event for given fd_event
-*/
-static void epoll_del_event(struct std_event_context *std_ev, struct tevent_fd *fde)
-{
-	struct epoll_event event;
-	if (std_ev->epoll_fd == -1) return;
-
-	fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-
-	/* if there's no epoll_event, we don't need to delete it */
-	if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
-
-	ZERO_STRUCT(event);
-	event.events = epoll_map_flags(fde->flags);
-	event.data.ptr = fde;
-	epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event);
-	fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
-}
-
-/*
- change the epoll event to the given fd_event
-*/
-static void epoll_mod_event(struct std_event_context *std_ev, struct tevent_fd *fde)
-{
-	struct epoll_event event;
-	if (std_ev->epoll_fd == -1) return;
-
-	fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-
-	ZERO_STRUCT(event);
-	event.events = epoll_map_flags(fde->flags);
-	event.data.ptr = fde;
-	if (epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
-		epoll_fallback_to_select(std_ev, "EPOLL_CTL_MOD failed");
-	}
-
-	/* only if we want to read we want to tell the event handler about errors */
-	if (fde->flags & TEVENT_FD_READ) {
-		fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-	}
-}
-
-static void epoll_change_event(struct std_event_context *std_ev, struct tevent_fd *fde)
-{
-	bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
-	bool want_read = (fde->flags & TEVENT_FD_READ);
-	bool want_write= (fde->flags & TEVENT_FD_WRITE);
-
-	if (std_ev->epoll_fd == -1) return;
-
-	fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
-
-	/* there's already an event */
-	if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
-		if (want_read || (want_write && !got_error)) {
-			epoll_mod_event(std_ev, fde);
-			return;
-		}
-		/* 
-		 * if we want to match the select behavior, we need to remove the epoll_event
-		 * when the caller isn't interested in events.
-		 *
-		 * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
-		 */
-		epoll_del_event(std_ev, fde);
-		return;
-	}
-
-	/* there's no epoll_event attached to the fde */
-	if (want_read || (want_write && !got_error)) {
-		epoll_add_event(std_ev, fde);
-		return;
-	}
-}
-
-/*
-  event loop handling using epoll
-*/
-static int epoll_event_loop(struct std_event_context *std_ev, struct timeval *tvalp)
-{
-	int ret, i;
-#define MAXEVENTS 1
-	struct epoll_event events[MAXEVENTS];
-	int timeout = -1;
-
-	if (std_ev->epoll_fd == -1) return -1;
-
-	if (tvalp) {
-		/* it's better to trigger timed events a bit later than to early */
-		timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
-	}
-
-	if (std_ev->ev->signal_events &&
-	    tevent_common_check_signal(std_ev->ev)) {
-		return 0;
-	}
-
-	tevent_trace_point_callback(std_ev->ev, TEVENT_TRACE_BEFORE_WAIT);
-	ret = epoll_wait(std_ev->epoll_fd, events, MAXEVENTS, timeout);
-	tevent_trace_point_callback(std_ev->ev, TEVENT_TRACE_AFTER_WAIT);
-
-	if (ret == -1 && errno == EINTR && std_ev->ev->signal_events) {
-		if (tevent_common_check_signal(std_ev->ev)) {
-			return 0;
-		}
-	}
-
-	if (ret == -1 && errno != EINTR) {
-		epoll_fallback_to_select(std_ev, "epoll_wait() failed");
-		return -1;
-	}
-
-	if (ret == 0 && tvalp) {
-		/* we don't care about a possible delay here */
-		tevent_common_loop_timer_delay(std_ev->ev);
-		return 0;
-	}
-
-	for (i=0;i<ret;i++) {
-		struct tevent_fd *fde = talloc_get_type(events[i].data.ptr, 
-						       struct tevent_fd);
-		uint16_t flags = 0;
-
-		if (fde == NULL) {
-			epoll_fallback_to_select(std_ev, "epoll_wait() gave bad data");
-			return -1;
-		}
-		if (events[i].events & (EPOLLHUP|EPOLLERR)) {
-			fde->additional_flags |= EPOLL_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 epoll_event,
-			 * as we only report errors when waiting for read events,
-			 * to match the select() behavior
-			 */
-			if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
-				epoll_del_event(std_ev, fde);
-				continue;
-			}
-			flags |= TEVENT_FD_READ;
-		}
-		if (events[i].events & EPOLLIN) flags |= TEVENT_FD_READ;
-		if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE;
-		if (flags) {
-			fde->handler(std_ev->ev, fde, flags, fde->private_data);
-			break;
-		}
-	}
-
-	return 0;
-}
-#else
-#define epoll_init_ctx(std_ev) 
-#define epoll_add_event(std_ev,fde)
-#define epoll_del_event(std_ev,fde)
-#define epoll_change_event(std_ev,fde)
-#define epoll_event_loop(std_ev,tvalp) (-1)
-#define epoll_check_reopen(std_ev)
-#endif
-
-/*
-  create a std_event_context structure.
-*/
-static int std_event_context_init(struct tevent_context *ev)
-{
-	struct std_event_context *std_ev;
-
-	std_ev = talloc_zero(ev, struct std_event_context);
-	if (!std_ev) return -1;
-	std_ev->ev = ev;
-	std_ev->epoll_fd = -1;
-
-	epoll_init_ctx(std_ev);
-
-	ev->additional_data = std_ev;
-	return 0;
-}
-
-/*
-  recalculate the maxfd
-*/
-static void calc_maxfd(struct std_event_context *std_ev)
-{
-	struct tevent_fd *fde;
-
-	std_ev->maxfd = 0;
-	for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
-		if (fde->fd > std_ev->maxfd) {
-			std_ev->maxfd = fde->fd;
-		}
-	}
-}
-
-
-/* to mark the ev->maxfd invalid
- * this means we need to recalculate it
- */
-#define EVENT_INVALID_MAXFD (-1)
-
-/*
-  destroy an fd_event
-*/
-static int std_event_fd_destructor(struct tevent_fd *fde)
-{
-	struct tevent_context *ev = fde->event_ctx;
-	struct std_event_context *std_ev = NULL;
-
-	if (ev) {
-		std_ev = talloc_get_type(ev->additional_data,
-					 struct std_event_context);
-
-		epoll_check_reopen(std_ev);
-
-		if (std_ev->maxfd == fde->fd) {
-			std_ev->maxfd = EVENT_INVALID_MAXFD;
-		}
-
-		epoll_del_event(std_ev, fde);
-	}
-
-	return tevent_common_fd_destructor(fde);
-}
-
-/*
-  add a fd based event
-  return NULL on failure (memory allocation error)
-*/
-static struct tevent_fd *std_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 std_event_context *std_ev = talloc_get_type(ev->additional_data,
-							   struct std_event_context);
-	struct tevent_fd *fde;
-
-	epoll_check_reopen(std_ev);
-
-	fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
-				   handler, private_data,
-				   handler_name, location);
-	if (!fde) return NULL;
-
-	if ((std_ev->maxfd != EVENT_INVALID_MAXFD)
-	    && (fde->fd > std_ev->maxfd)) {
-		std_ev->maxfd = fde->fd;
-	}
-	talloc_set_destructor(fde, std_event_fd_destructor);
-
-	epoll_add_event(std_ev, fde);
-
-	return fde;
-}
-
-/*
-  set the fd event flags
-*/
-static void std_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
-{
-	struct tevent_context *ev;
-	struct std_event_context *std_ev;
-
-	if (fde->flags == flags) return;
-
-	ev = fde->event_ctx;
-	std_ev = talloc_get_type(ev->additional_data, struct std_event_context);
-
-	fde->flags = flags;
-
-	epoll_check_reopen(std_ev);
-
-	epoll_change_event(std_ev, fde);
-}
-
-/*
-  event loop handling using select()
-*/
-static int std_event_loop_select(struct std_event_context *std_ev, struct timeval *tvalp)
-{
-	fd_set r_fds, w_fds;
-	struct tevent_fd *fde;
-	int selrtn;
-
-	/* we maybe need to recalculate the maxfd */
-	if (std_ev->maxfd == EVENT_INVALID_MAXFD) {
-		calc_maxfd(std_ev);
-	}
-
-	FD_ZERO(&r_fds);
-	FD_ZERO(&w_fds);
-
-	/* setup any fd events */
-	for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
-		if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
-			std_ev->exit_code = EBADF;
-			return -1;
-		}
-		if (fde->flags & TEVENT_FD_READ) {
-			FD_SET(fde->fd, &r_fds);
-		}
-		if (fde->flags & TEVENT_FD_WRITE) {
-			FD_SET(fde->fd, &w_fds);
-		}
-	}
-
-	if (std_ev->ev->signal_events &&
-	    tevent_common_check_signal(std_ev->ev)) {
-		return 0;
-	}
-
-	selrtn = select(std_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
-
-	if (selrtn == -1 && errno == EINTR && 
-	    std_ev->ev->signal_events) {
-		tevent_common_check_signal(std_ev->ev);
-		return 0;
-	}
-
-	if (selrtn == -1 && errno == EBADF) {
-		/* the socket is dead! this should never
-		   happen as the socket should have first been
-		   made readable and that should have removed
-		   the event, so this must be a bug. This is a
-		   fatal error. */
-		tevent_debug(std_ev->ev, TEVENT_DEBUG_FATAL,
-			     "ERROR: EBADF on std_event_loop_once\n");
-		std_ev->exit_code = EBADF;
-		return -1;
-	}
-
-	if (selrtn == 0 && tvalp) {
-		/* we don't care about a possible delay here */
-		tevent_common_loop_timer_delay(std_ev->ev);
-		return 0;
-	}
-
-	if (selrtn > 0) {
-		/* at least one file descriptor is ready - check
-		   which ones and call the handler, being careful to allow
-		   the handler to remove itself when called */
-		for (fde = std_ev->ev->fd_events; fde; fde = fde->next) {
-			uint16_t flags = 0;
-
-			if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
-			if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
-			if (flags & fde->flags) {
-				fde->handler(std_ev->ev, fde, flags, fde->private_data);
-				break;
-			}
-		}
-	}
-
-	return 0;
-}		
-
-/*
-  do a single event loop using the events defined in ev 
-*/
-static int std_event_loop_once(struct tevent_context *ev, const char *location)
-{
-	struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
-		 					   struct std_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;
-	}
-
-	epoll_check_reopen(std_ev);
-
-	if (epoll_event_loop(std_ev, &tval) == 0) {
-		return 0;
-	}
-
-	return std_event_loop_select(std_ev, &tval);
-}
-
-static const struct tevent_ops std_event_ops = {
-	.context_init		= std_event_context_init,
-	.add_fd			= std_event_add_fd,
-	.set_fd_close_fn	= tevent_common_fd_set_close_fn,
-	.get_fd_flags		= tevent_common_fd_get_flags,
-	.set_fd_flags		= std_event_set_fd_flags,
-	.add_timer		= tevent_common_add_timer,
-	.schedule_immediate	= tevent_common_schedule_immediate,
-	.add_signal		= tevent_common_add_signal,
-	.loop_once		= std_event_loop_once,
-	.loop_wait		= tevent_common_loop_wait,
-};
-#endif
-
 /*
   If this function gets called. epoll failed at runtime.
   Move us to using poll instead. If we return false here,
-- 
1.7.10.4



More information about the samba-technical mailing list