tevent: pass 'bool replay' to epoll_panic()
[Samba/gebeck_regimport.git] / lib / tevent / tevent_epoll.c
blobc7936d3c949a0c8220dc93bdabfa9f969eb84d0d
1 /*
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
11 ** under the LGPL
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/>.
27 #include "replace.h"
28 #include "system/filesys.h"
29 #include "system/select.h"
30 #include "tevent.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 */
39 int epoll_fd;
41 pid_t pid;
45 called when a epoll call fails
47 static void epoll_panic(struct epoll_event_context *epoll_ev,
48 const char *reason, bool replay)
50 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
51 "%s (%s) - calling abort()\n", reason, strerror(errno));
52 abort();
56 map from TEVENT_FD_* to EPOLLIN/EPOLLOUT
58 static uint32_t epoll_map_flags(uint16_t flags)
60 uint32_t ret = 0;
61 if (flags & TEVENT_FD_READ) ret |= (EPOLLIN | EPOLLERR | EPOLLHUP);
62 if (flags & TEVENT_FD_WRITE) ret |= (EPOLLOUT | EPOLLERR | EPOLLHUP);
63 return ret;
67 free the epoll fd
69 static int epoll_ctx_destructor(struct epoll_event_context *epoll_ev)
71 close(epoll_ev->epoll_fd);
72 epoll_ev->epoll_fd = -1;
73 return 0;
77 init the epoll fd
79 static int epoll_init_ctx(struct epoll_event_context *epoll_ev)
81 epoll_ev->epoll_fd = epoll_create(64);
82 if (epoll_ev->epoll_fd == -1) {
83 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
84 "Failed to create epoll handle.\n");
85 return -1;
88 if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
89 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
90 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
93 epoll_ev->pid = getpid();
94 talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
96 return 0;
99 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde);
102 reopen the epoll handle when our pid changes
103 see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an
104 demonstration of why this is needed
106 static void epoll_check_reopen(struct epoll_event_context *epoll_ev)
108 struct tevent_fd *fde;
110 if (epoll_ev->pid == getpid()) {
111 return;
114 close(epoll_ev->epoll_fd);
115 epoll_ev->epoll_fd = epoll_create(64);
116 if (epoll_ev->epoll_fd == -1) {
117 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
118 "Failed to recreate epoll handle after fork\n");
119 return;
122 if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
123 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
124 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
127 epoll_ev->pid = getpid();
128 for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) {
129 epoll_add_event(epoll_ev, fde);
133 #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT (1<<0)
134 #define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR (1<<1)
135 #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR (1<<2)
138 add the epoll event to the given fd_event
140 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
142 struct epoll_event event;
144 if (epoll_ev->epoll_fd == -1) return;
146 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
148 /* if we don't want events yet, don't add an epoll_event */
149 if (fde->flags == 0) return;
151 ZERO_STRUCT(event);
152 event.events = epoll_map_flags(fde->flags);
153 event.data.ptr = fde;
154 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
155 epoll_panic(epoll_ev, "EPOLL_CTL_ADD failed", false);
156 return;
158 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
160 /* only if we want to read we want to tell the event handler about errors */
161 if (fde->flags & TEVENT_FD_READ) {
162 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
167 delete the epoll event for given fd_event
169 static void epoll_del_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
171 struct epoll_event event;
173 if (epoll_ev->epoll_fd == -1) return;
175 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
177 /* if there's no epoll_event, we don't need to delete it */
178 if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
180 ZERO_STRUCT(event);
181 event.events = epoll_map_flags(fde->flags);
182 event.data.ptr = fde;
183 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event) != 0) {
184 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
185 "epoll_del_event failed! probable early close bug (%s)\n",
186 strerror(errno));
188 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
192 change the epoll event to the given fd_event
194 static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
196 struct epoll_event event;
197 if (epoll_ev->epoll_fd == -1) return;
199 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
201 ZERO_STRUCT(event);
202 event.events = epoll_map_flags(fde->flags);
203 event.data.ptr = fde;
204 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
205 epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed", false);
206 return;
209 /* only if we want to read we want to tell the event handler about errors */
210 if (fde->flags & TEVENT_FD_READ) {
211 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
215 static void epoll_change_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
217 bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
218 bool want_read = (fde->flags & TEVENT_FD_READ);
219 bool want_write= (fde->flags & TEVENT_FD_WRITE);
221 if (epoll_ev->epoll_fd == -1) return;
223 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
225 /* there's already an event */
226 if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
227 if (want_read || (want_write && !got_error)) {
228 epoll_mod_event(epoll_ev, fde);
229 return;
232 * if we want to match the select behavior, we need to remove the epoll_event
233 * when the caller isn't interested in events.
235 * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
237 epoll_del_event(epoll_ev, fde);
238 return;
241 /* there's no epoll_event attached to the fde */
242 if (want_read || (want_write && !got_error)) {
243 epoll_add_event(epoll_ev, fde);
244 return;
249 event loop handling using epoll
251 static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp)
253 int ret, i;
254 #define MAXEVENTS 1
255 struct epoll_event events[MAXEVENTS];
256 int timeout = -1;
258 if (epoll_ev->epoll_fd == -1) return -1;
260 if (tvalp) {
261 /* it's better to trigger timed events a bit later than to early */
262 timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
265 if (epoll_ev->ev->signal_events &&
266 tevent_common_check_signal(epoll_ev->ev)) {
267 return 0;
270 tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_BEFORE_WAIT);
271 ret = epoll_wait(epoll_ev->epoll_fd, events, MAXEVENTS, timeout);
272 tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_AFTER_WAIT);
274 if (ret == -1 && errno == EINTR && epoll_ev->ev->signal_events) {
275 if (tevent_common_check_signal(epoll_ev->ev)) {
276 return 0;
280 if (ret == -1 && errno != EINTR) {
281 epoll_panic(epoll_ev, "epoll_wait() failed", true);
282 return -1;
285 if (ret == 0 && tvalp) {
286 /* we don't care about a possible delay here */
287 tevent_common_loop_timer_delay(epoll_ev->ev);
288 return 0;
291 for (i=0;i<ret;i++) {
292 struct tevent_fd *fde = talloc_get_type(events[i].data.ptr,
293 struct tevent_fd);
294 uint16_t flags = 0;
296 if (fde == NULL) {
297 epoll_panic(epoll_ev, "epoll_wait() gave bad data", true);
298 return -1;
300 if (events[i].events & (EPOLLHUP|EPOLLERR)) {
301 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR;
303 * if we only wait for TEVENT_FD_WRITE, we should not tell the
304 * event handler about it, and remove the epoll_event,
305 * as we only report errors when waiting for read events,
306 * to match the select() behavior
308 if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
309 epoll_del_event(epoll_ev, fde);
310 continue;
312 flags |= TEVENT_FD_READ;
314 if (events[i].events & EPOLLIN) flags |= TEVENT_FD_READ;
315 if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE;
316 if (flags) {
317 fde->handler(epoll_ev->ev, fde, flags, fde->private_data);
318 break;
322 return 0;
326 create a epoll_event_context structure.
328 static int epoll_event_context_init(struct tevent_context *ev)
330 int ret;
331 struct epoll_event_context *epoll_ev;
333 epoll_ev = talloc_zero(ev, struct epoll_event_context);
334 if (!epoll_ev) return -1;
335 epoll_ev->ev = ev;
336 epoll_ev->epoll_fd = -1;
338 ret = epoll_init_ctx(epoll_ev);
339 if (ret != 0) {
340 talloc_free(epoll_ev);
341 return ret;
344 ev->additional_data = epoll_ev;
345 return 0;
349 destroy an fd_event
351 static int epoll_event_fd_destructor(struct tevent_fd *fde)
353 struct tevent_context *ev = fde->event_ctx;
354 struct epoll_event_context *epoll_ev = NULL;
356 if (ev) {
357 epoll_ev = talloc_get_type(ev->additional_data,
358 struct epoll_event_context);
360 epoll_check_reopen(epoll_ev);
362 epoll_del_event(epoll_ev, fde);
365 return tevent_common_fd_destructor(fde);
369 add a fd based event
370 return NULL on failure (memory allocation error)
372 static struct tevent_fd *epoll_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
373 int fd, uint16_t flags,
374 tevent_fd_handler_t handler,
375 void *private_data,
376 const char *handler_name,
377 const char *location)
379 struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
380 struct epoll_event_context);
381 struct tevent_fd *fde;
383 epoll_check_reopen(epoll_ev);
385 fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
386 handler, private_data,
387 handler_name, location);
388 if (!fde) return NULL;
390 talloc_set_destructor(fde, epoll_event_fd_destructor);
392 epoll_add_event(epoll_ev, fde);
394 return fde;
398 set the fd event flags
400 static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
402 struct tevent_context *ev;
403 struct epoll_event_context *epoll_ev;
405 if (fde->flags == flags) return;
407 ev = fde->event_ctx;
408 epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context);
410 fde->flags = flags;
412 epoll_check_reopen(epoll_ev);
414 epoll_change_event(epoll_ev, fde);
418 do a single event loop using the events defined in ev
420 static int epoll_event_loop_once(struct tevent_context *ev, const char *location)
422 struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
423 struct epoll_event_context);
424 struct timeval tval;
426 if (ev->signal_events &&
427 tevent_common_check_signal(ev)) {
428 return 0;
431 if (ev->immediate_events &&
432 tevent_common_loop_immediate(ev)) {
433 return 0;
436 tval = tevent_common_loop_timer_delay(ev);
437 if (tevent_timeval_is_zero(&tval)) {
438 return 0;
441 epoll_check_reopen(epoll_ev);
443 return epoll_event_loop(epoll_ev, &tval);
446 static const struct tevent_ops epoll_event_ops = {
447 .context_init = epoll_event_context_init,
448 .add_fd = epoll_event_add_fd,
449 .set_fd_close_fn = tevent_common_fd_set_close_fn,
450 .get_fd_flags = tevent_common_fd_get_flags,
451 .set_fd_flags = epoll_event_set_fd_flags,
452 .add_timer = tevent_common_add_timer,
453 .schedule_immediate = tevent_common_schedule_immediate,
454 .add_signal = tevent_common_add_signal,
455 .loop_once = epoll_event_loop_once,
456 .loop_wait = tevent_common_loop_wait,
459 _PRIVATE_ bool tevent_epoll_init(void)
461 return tevent_register_backend("epoll", &epoll_event_ops);