2 Unix SMB/CIFS implementation.
4 main select loop and event handling - epoll implementation
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan Metzmacher 2005-2009
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "system/filesys.h"
29 #include "system/select.h"
31 #include "tevent_internal.h"
32 #include "tevent_util.h"
34 struct epoll_event_context
{
35 /* a pointer back to the generic event_context */
36 struct tevent_context
*ev
;
38 /* when using epoll this is the handle from epoll_create */
43 bool (*panic_fallback
)(struct tevent_context
*ev
, bool replay
);
47 called to set the panic fallback function.
49 _PRIVATE_
bool tevent_epoll_set_panic_fallback(struct tevent_context
*ev
,
50 bool (*panic_fallback
)(struct tevent_context
*ev
,
53 struct epoll_event_context
*epoll_ev
;
55 if (ev
->additional_data
== NULL
) {
59 epoll_ev
= talloc_get_type(ev
->additional_data
,
60 struct epoll_event_context
);
61 if (epoll_ev
== NULL
) {
64 epoll_ev
->panic_fallback
= panic_fallback
;
69 called when a epoll call fails
71 static void epoll_panic(struct epoll_event_context
*epoll_ev
,
72 const char *reason
, bool replay
)
74 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_FATAL
,
75 "%s (%s) - calling abort()\n", reason
, strerror(errno
));
80 map from TEVENT_FD_* to EPOLLIN/EPOLLOUT
82 static uint32_t epoll_map_flags(uint16_t flags
)
85 if (flags
& TEVENT_FD_READ
) ret
|= (EPOLLIN
| EPOLLERR
| EPOLLHUP
);
86 if (flags
& TEVENT_FD_WRITE
) ret
|= (EPOLLOUT
| EPOLLERR
| EPOLLHUP
);
93 static int epoll_ctx_destructor(struct epoll_event_context
*epoll_ev
)
95 close(epoll_ev
->epoll_fd
);
96 epoll_ev
->epoll_fd
= -1;
103 static int epoll_init_ctx(struct epoll_event_context
*epoll_ev
)
105 epoll_ev
->epoll_fd
= epoll_create(64);
106 if (epoll_ev
->epoll_fd
== -1) {
107 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_FATAL
,
108 "Failed to create epoll handle.\n");
112 if (!ev_set_close_on_exec(epoll_ev
->epoll_fd
)) {
113 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_WARNING
,
114 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
117 epoll_ev
->pid
= getpid();
118 talloc_set_destructor(epoll_ev
, epoll_ctx_destructor
);
123 static void epoll_add_event(struct epoll_event_context
*epoll_ev
, struct tevent_fd
*fde
);
126 reopen the epoll handle when our pid changes
127 see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an
128 demonstration of why this is needed
130 static void epoll_check_reopen(struct epoll_event_context
*epoll_ev
)
132 struct tevent_fd
*fde
;
134 if (epoll_ev
->pid
== getpid()) {
138 close(epoll_ev
->epoll_fd
);
139 epoll_ev
->epoll_fd
= epoll_create(64);
140 if (epoll_ev
->epoll_fd
== -1) {
141 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_FATAL
,
142 "Failed to recreate epoll handle after fork\n");
146 if (!ev_set_close_on_exec(epoll_ev
->epoll_fd
)) {
147 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_WARNING
,
148 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
151 epoll_ev
->pid
= getpid();
152 for (fde
=epoll_ev
->ev
->fd_events
;fde
;fde
=fde
->next
) {
153 epoll_add_event(epoll_ev
, fde
);
157 #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT (1<<0)
158 #define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR (1<<1)
159 #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR (1<<2)
162 add the epoll event to the given fd_event
164 static void epoll_add_event(struct epoll_event_context
*epoll_ev
, struct tevent_fd
*fde
)
166 struct epoll_event event
;
168 if (epoll_ev
->epoll_fd
== -1) return;
170 fde
->additional_flags
&= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
172 /* if we don't want events yet, don't add an epoll_event */
173 if (fde
->flags
== 0) return;
176 event
.events
= epoll_map_flags(fde
->flags
);
177 event
.data
.ptr
= fde
;
178 if (epoll_ctl(epoll_ev
->epoll_fd
, EPOLL_CTL_ADD
, fde
->fd
, &event
) != 0) {
179 epoll_panic(epoll_ev
, "EPOLL_CTL_ADD failed", false);
182 fde
->additional_flags
|= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT
;
184 /* only if we want to read we want to tell the event handler about errors */
185 if (fde
->flags
& TEVENT_FD_READ
) {
186 fde
->additional_flags
|= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
191 delete the epoll event for given fd_event
193 static void epoll_del_event(struct epoll_event_context
*epoll_ev
, struct tevent_fd
*fde
)
195 struct epoll_event event
;
197 if (epoll_ev
->epoll_fd
== -1) return;
199 fde
->additional_flags
&= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
201 /* if there's no epoll_event, we don't need to delete it */
202 if (!(fde
->additional_flags
& EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT
)) return;
205 event
.events
= epoll_map_flags(fde
->flags
);
206 event
.data
.ptr
= fde
;
207 if (epoll_ctl(epoll_ev
->epoll_fd
, EPOLL_CTL_DEL
, fde
->fd
, &event
) != 0) {
208 tevent_debug(epoll_ev
->ev
, TEVENT_DEBUG_FATAL
,
209 "epoll_del_event failed! probable early close bug (%s)\n",
212 fde
->additional_flags
&= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT
;
216 change the epoll event to the given fd_event
218 static void epoll_mod_event(struct epoll_event_context
*epoll_ev
, struct tevent_fd
*fde
)
220 struct epoll_event event
;
221 if (epoll_ev
->epoll_fd
== -1) return;
223 fde
->additional_flags
&= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
226 event
.events
= epoll_map_flags(fde
->flags
);
227 event
.data
.ptr
= fde
;
228 if (epoll_ctl(epoll_ev
->epoll_fd
, EPOLL_CTL_MOD
, fde
->fd
, &event
) != 0) {
229 epoll_panic(epoll_ev
, "EPOLL_CTL_MOD failed", false);
233 /* only if we want to read we want to tell the event handler about errors */
234 if (fde
->flags
& TEVENT_FD_READ
) {
235 fde
->additional_flags
|= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
239 static void epoll_change_event(struct epoll_event_context
*epoll_ev
, struct tevent_fd
*fde
)
241 bool got_error
= (fde
->additional_flags
& EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR
);
242 bool want_read
= (fde
->flags
& TEVENT_FD_READ
);
243 bool want_write
= (fde
->flags
& TEVENT_FD_WRITE
);
245 if (epoll_ev
->epoll_fd
== -1) return;
247 fde
->additional_flags
&= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
;
249 /* there's already an event */
250 if (fde
->additional_flags
& EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT
) {
251 if (want_read
|| (want_write
&& !got_error
)) {
252 epoll_mod_event(epoll_ev
, fde
);
256 * if we want to match the select behavior, we need to remove the epoll_event
257 * when the caller isn't interested in events.
259 * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
261 epoll_del_event(epoll_ev
, fde
);
265 /* there's no epoll_event attached to the fde */
266 if (want_read
|| (want_write
&& !got_error
)) {
267 epoll_add_event(epoll_ev
, fde
);
273 event loop handling using epoll
275 static int epoll_event_loop(struct epoll_event_context
*epoll_ev
, struct timeval
*tvalp
)
279 struct epoll_event events
[MAXEVENTS
];
282 if (epoll_ev
->epoll_fd
== -1) return -1;
285 /* it's better to trigger timed events a bit later than to early */
286 timeout
= ((tvalp
->tv_usec
+999) / 1000) + (tvalp
->tv_sec
*1000);
289 if (epoll_ev
->ev
->signal_events
&&
290 tevent_common_check_signal(epoll_ev
->ev
)) {
294 tevent_trace_point_callback(epoll_ev
->ev
, TEVENT_TRACE_BEFORE_WAIT
);
295 ret
= epoll_wait(epoll_ev
->epoll_fd
, events
, MAXEVENTS
, timeout
);
296 tevent_trace_point_callback(epoll_ev
->ev
, TEVENT_TRACE_AFTER_WAIT
);
298 if (ret
== -1 && errno
== EINTR
&& epoll_ev
->ev
->signal_events
) {
299 if (tevent_common_check_signal(epoll_ev
->ev
)) {
304 if (ret
== -1 && errno
!= EINTR
) {
305 epoll_panic(epoll_ev
, "epoll_wait() failed", true);
309 if (ret
== 0 && tvalp
) {
310 /* we don't care about a possible delay here */
311 tevent_common_loop_timer_delay(epoll_ev
->ev
);
315 for (i
=0;i
<ret
;i
++) {
316 struct tevent_fd
*fde
= talloc_get_type(events
[i
].data
.ptr
,
321 epoll_panic(epoll_ev
, "epoll_wait() gave bad data", true);
324 if (events
[i
].events
& (EPOLLHUP
|EPOLLERR
)) {
325 fde
->additional_flags
|= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR
;
327 * if we only wait for TEVENT_FD_WRITE, we should not tell the
328 * event handler about it, and remove the epoll_event,
329 * as we only report errors when waiting for read events,
330 * to match the select() behavior
332 if (!(fde
->additional_flags
& EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR
)) {
333 epoll_del_event(epoll_ev
, fde
);
336 flags
|= TEVENT_FD_READ
;
338 if (events
[i
].events
& EPOLLIN
) flags
|= TEVENT_FD_READ
;
339 if (events
[i
].events
& EPOLLOUT
) flags
|= TEVENT_FD_WRITE
;
341 fde
->handler(epoll_ev
->ev
, fde
, flags
, fde
->private_data
);
350 create a epoll_event_context structure.
352 static int epoll_event_context_init(struct tevent_context
*ev
)
355 struct epoll_event_context
*epoll_ev
;
357 epoll_ev
= talloc_zero(ev
, struct epoll_event_context
);
358 if (!epoll_ev
) return -1;
360 epoll_ev
->epoll_fd
= -1;
362 ret
= epoll_init_ctx(epoll_ev
);
364 talloc_free(epoll_ev
);
368 ev
->additional_data
= epoll_ev
;
375 static int epoll_event_fd_destructor(struct tevent_fd
*fde
)
377 struct tevent_context
*ev
= fde
->event_ctx
;
378 struct epoll_event_context
*epoll_ev
= NULL
;
381 epoll_ev
= talloc_get_type(ev
->additional_data
,
382 struct epoll_event_context
);
384 epoll_check_reopen(epoll_ev
);
386 epoll_del_event(epoll_ev
, fde
);
389 return tevent_common_fd_destructor(fde
);
394 return NULL on failure (memory allocation error)
396 static struct tevent_fd
*epoll_event_add_fd(struct tevent_context
*ev
, TALLOC_CTX
*mem_ctx
,
397 int fd
, uint16_t flags
,
398 tevent_fd_handler_t handler
,
400 const char *handler_name
,
401 const char *location
)
403 struct epoll_event_context
*epoll_ev
= talloc_get_type(ev
->additional_data
,
404 struct epoll_event_context
);
405 struct tevent_fd
*fde
;
407 epoll_check_reopen(epoll_ev
);
409 fde
= tevent_common_add_fd(ev
, mem_ctx
, fd
, flags
,
410 handler
, private_data
,
411 handler_name
, location
);
412 if (!fde
) return NULL
;
414 talloc_set_destructor(fde
, epoll_event_fd_destructor
);
416 epoll_add_event(epoll_ev
, fde
);
422 set the fd event flags
424 static void epoll_event_set_fd_flags(struct tevent_fd
*fde
, uint16_t flags
)
426 struct tevent_context
*ev
;
427 struct epoll_event_context
*epoll_ev
;
429 if (fde
->flags
== flags
) return;
432 epoll_ev
= talloc_get_type(ev
->additional_data
, struct epoll_event_context
);
436 epoll_check_reopen(epoll_ev
);
438 epoll_change_event(epoll_ev
, fde
);
442 do a single event loop using the events defined in ev
444 static int epoll_event_loop_once(struct tevent_context
*ev
, const char *location
)
446 struct epoll_event_context
*epoll_ev
= talloc_get_type(ev
->additional_data
,
447 struct epoll_event_context
);
450 if (ev
->signal_events
&&
451 tevent_common_check_signal(ev
)) {
455 if (ev
->immediate_events
&&
456 tevent_common_loop_immediate(ev
)) {
460 tval
= tevent_common_loop_timer_delay(ev
);
461 if (tevent_timeval_is_zero(&tval
)) {
465 epoll_check_reopen(epoll_ev
);
467 return epoll_event_loop(epoll_ev
, &tval
);
470 static const struct tevent_ops epoll_event_ops
= {
471 .context_init
= epoll_event_context_init
,
472 .add_fd
= epoll_event_add_fd
,
473 .set_fd_close_fn
= tevent_common_fd_set_close_fn
,
474 .get_fd_flags
= tevent_common_fd_get_flags
,
475 .set_fd_flags
= epoll_event_set_fd_flags
,
476 .add_timer
= tevent_common_add_timer
,
477 .schedule_immediate
= tevent_common_schedule_immediate
,
478 .add_signal
= tevent_common_add_signal
,
479 .loop_once
= epoll_event_loop_once
,
480 .loop_wait
= tevent_common_loop_wait
,
483 _PRIVATE_
bool tevent_epoll_init(void)
485 return tevent_register_backend("epoll", &epoll_event_ops
);