Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / lib / tevent / tevent_epoll.c
blob33e1d3f20fffe6b6c0a970c9516591dfb78375c0
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, const char *reason)
49 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
50 "%s (%s) - calling abort()\n", reason, strerror(errno));
51 abort();
55 map from TEVENT_FD_* to EPOLLIN/EPOLLOUT
57 static uint32_t epoll_map_flags(uint16_t flags)
59 uint32_t ret = 0;
60 if (flags & TEVENT_FD_READ) ret |= (EPOLLIN | EPOLLERR | EPOLLHUP);
61 if (flags & TEVENT_FD_WRITE) ret |= (EPOLLOUT | EPOLLERR | EPOLLHUP);
62 return ret;
66 free the epoll fd
68 static int epoll_ctx_destructor(struct epoll_event_context *epoll_ev)
70 close(epoll_ev->epoll_fd);
71 epoll_ev->epoll_fd = -1;
72 return 0;
76 init the epoll fd
78 static int epoll_init_ctx(struct epoll_event_context *epoll_ev)
80 epoll_ev->epoll_fd = epoll_create(64);
81 if (epoll_ev->epoll_fd == -1) {
82 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
83 "Failed to create epoll handle.\n");
84 return -1;
87 if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
88 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
89 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
92 epoll_ev->pid = getpid();
93 talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
95 return 0;
98 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde);
101 reopen the epoll handle when our pid changes
102 see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an
103 demonstration of why this is needed
105 static void epoll_check_reopen(struct epoll_event_context *epoll_ev)
107 struct tevent_fd *fde;
109 if (epoll_ev->pid == getpid()) {
110 return;
113 close(epoll_ev->epoll_fd);
114 epoll_ev->epoll_fd = epoll_create(64);
115 if (epoll_ev->epoll_fd == -1) {
116 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
117 "Failed to recreate epoll handle after fork\n");
118 return;
121 if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
122 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
123 "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
126 epoll_ev->pid = getpid();
127 for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) {
128 epoll_add_event(epoll_ev, fde);
132 #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT (1<<0)
133 #define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR (1<<1)
134 #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR (1<<2)
137 add the epoll event to the given fd_event
139 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
141 struct epoll_event event;
143 if (epoll_ev->epoll_fd == -1) return;
145 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
147 /* if we don't want events yet, don't add an epoll_event */
148 if (fde->flags == 0) return;
150 ZERO_STRUCT(event);
151 event.events = epoll_map_flags(fde->flags);
152 event.data.ptr = fde;
153 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
154 epoll_panic(epoll_ev, "EPOLL_CTL_ADD failed");
156 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
158 /* only if we want to read we want to tell the event handler about errors */
159 if (fde->flags & TEVENT_FD_READ) {
160 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
165 delete the epoll event for given fd_event
167 static void epoll_del_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
169 struct epoll_event event;
171 if (epoll_ev->epoll_fd == -1) return;
173 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
175 /* if there's no epoll_event, we don't need to delete it */
176 if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
178 ZERO_STRUCT(event);
179 event.events = epoll_map_flags(fde->flags);
180 event.data.ptr = fde;
181 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event) != 0) {
182 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
183 "epoll_del_event failed! probable early close bug (%s)\n",
184 strerror(errno));
186 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
190 change the epoll event to the given fd_event
192 static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
194 struct epoll_event event;
195 if (epoll_ev->epoll_fd == -1) return;
197 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
199 ZERO_STRUCT(event);
200 event.events = epoll_map_flags(fde->flags);
201 event.data.ptr = fde;
202 if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
203 epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed");
206 /* only if we want to read we want to tell the event handler about errors */
207 if (fde->flags & TEVENT_FD_READ) {
208 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
212 static void epoll_change_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
214 bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
215 bool want_read = (fde->flags & TEVENT_FD_READ);
216 bool want_write= (fde->flags & TEVENT_FD_WRITE);
218 if (epoll_ev->epoll_fd == -1) return;
220 fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
222 /* there's already an event */
223 if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
224 if (want_read || (want_write && !got_error)) {
225 epoll_mod_event(epoll_ev, fde);
226 return;
229 * if we want to match the select behavior, we need to remove the epoll_event
230 * when the caller isn't interested in events.
232 * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
234 epoll_del_event(epoll_ev, fde);
235 return;
238 /* there's no epoll_event attached to the fde */
239 if (want_read || (want_write && !got_error)) {
240 epoll_add_event(epoll_ev, fde);
241 return;
246 event loop handling using epoll
248 static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp)
250 int ret, i;
251 #define MAXEVENTS 1
252 struct epoll_event events[MAXEVENTS];
253 int timeout = -1;
255 if (epoll_ev->epoll_fd == -1) return -1;
257 if (tvalp) {
258 /* it's better to trigger timed events a bit later than to early */
259 timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
262 if (epoll_ev->ev->signal_events &&
263 tevent_common_check_signal(epoll_ev->ev)) {
264 return 0;
267 ret = epoll_wait(epoll_ev->epoll_fd, events, MAXEVENTS, timeout);
269 if (ret == -1 && errno == EINTR && epoll_ev->ev->signal_events) {
270 if (tevent_common_check_signal(epoll_ev->ev)) {
271 return 0;
275 if (ret == -1 && errno != EINTR) {
276 epoll_panic(epoll_ev, "epoll_wait() failed");
277 return -1;
280 if (ret == 0 && tvalp) {
281 /* we don't care about a possible delay here */
282 tevent_common_loop_timer_delay(epoll_ev->ev);
283 return 0;
286 for (i=0;i<ret;i++) {
287 struct tevent_fd *fde = talloc_get_type(events[i].data.ptr,
288 struct tevent_fd);
289 uint16_t flags = 0;
291 if (fde == NULL) {
292 epoll_panic(epoll_ev, "epoll_wait() gave bad data");
293 return -1;
295 if (events[i].events & (EPOLLHUP|EPOLLERR)) {
296 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR;
298 * if we only wait for TEVENT_FD_WRITE, we should not tell the
299 * event handler about it, and remove the epoll_event,
300 * as we only report errors when waiting for read events,
301 * to match the select() behavior
303 if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
304 epoll_del_event(epoll_ev, fde);
305 continue;
307 flags |= TEVENT_FD_READ;
309 if (events[i].events & EPOLLIN) flags |= TEVENT_FD_READ;
310 if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE;
311 if (flags) {
312 fde->handler(epoll_ev->ev, fde, flags, fde->private_data);
313 break;
317 return 0;
321 create a epoll_event_context structure.
323 static int epoll_event_context_init(struct tevent_context *ev)
325 int ret;
326 struct epoll_event_context *epoll_ev;
328 epoll_ev = talloc_zero(ev, struct epoll_event_context);
329 if (!epoll_ev) return -1;
330 epoll_ev->ev = ev;
331 epoll_ev->epoll_fd = -1;
333 ret = epoll_init_ctx(epoll_ev);
334 if (ret != 0) {
335 talloc_free(epoll_ev);
336 return ret;
339 ev->additional_data = epoll_ev;
340 return 0;
344 destroy an fd_event
346 static int epoll_event_fd_destructor(struct tevent_fd *fde)
348 struct tevent_context *ev = fde->event_ctx;
349 struct epoll_event_context *epoll_ev = NULL;
351 if (ev) {
352 epoll_ev = talloc_get_type(ev->additional_data,
353 struct epoll_event_context);
355 epoll_check_reopen(epoll_ev);
357 epoll_del_event(epoll_ev, fde);
360 return tevent_common_fd_destructor(fde);
364 add a fd based event
365 return NULL on failure (memory allocation error)
367 static struct tevent_fd *epoll_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
368 int fd, uint16_t flags,
369 tevent_fd_handler_t handler,
370 void *private_data,
371 const char *handler_name,
372 const char *location)
374 struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
375 struct epoll_event_context);
376 struct tevent_fd *fde;
378 epoll_check_reopen(epoll_ev);
380 fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
381 handler, private_data,
382 handler_name, location);
383 if (!fde) return NULL;
385 talloc_set_destructor(fde, epoll_event_fd_destructor);
387 epoll_add_event(epoll_ev, fde);
389 return fde;
393 set the fd event flags
395 static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
397 struct tevent_context *ev;
398 struct epoll_event_context *epoll_ev;
400 if (fde->flags == flags) return;
402 ev = fde->event_ctx;
403 epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context);
405 fde->flags = flags;
407 epoll_check_reopen(epoll_ev);
409 epoll_change_event(epoll_ev, fde);
413 do a single event loop using the events defined in ev
415 static int epoll_event_loop_once(struct tevent_context *ev, const char *location)
417 struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
418 struct epoll_event_context);
419 struct timeval tval;
421 if (ev->signal_events &&
422 tevent_common_check_signal(ev)) {
423 return 0;
426 if (ev->immediate_events &&
427 tevent_common_loop_immediate(ev)) {
428 return 0;
431 tval = tevent_common_loop_timer_delay(ev);
432 if (tevent_timeval_is_zero(&tval)) {
433 return 0;
436 epoll_check_reopen(epoll_ev);
438 return epoll_event_loop(epoll_ev, &tval);
441 static const struct tevent_ops epoll_event_ops = {
442 .context_init = epoll_event_context_init,
443 .add_fd = epoll_event_add_fd,
444 .set_fd_close_fn = tevent_common_fd_set_close_fn,
445 .get_fd_flags = tevent_common_fd_get_flags,
446 .set_fd_flags = epoll_event_set_fd_flags,
447 .add_timer = tevent_common_add_timer,
448 .schedule_immediate = tevent_common_schedule_immediate,
449 .add_signal = tevent_common_add_signal,
450 .loop_once = epoll_event_loop_once,
451 .loop_wait = tevent_common_loop_wait,
454 _PRIVATE_ bool tevent_epoll_init(void)
456 return tevent_register_backend("epoll", &epoll_event_ops);