3ab828395586f6a8ce1d4563678b58ad24cd7ec7
[sfrench/samba-autobuild/.git] / lib / tevent / tevent_epoll.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    main select loop and event handling - epoll implementation
5
6    Copyright (C) Andrew Tridgell        2003-2005
7    Copyright (C) Stefan Metzmacher      2005-2009
8
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
12
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.
17
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.
22
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/>.
25 */
26
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"
33
34 struct epoll_event_context {
35         /* a pointer back to the generic event_context */
36         struct tevent_context *ev;
37
38         /* when using epoll this is the handle from epoll_create */
39         int epoll_fd;
40
41         pid_t pid;
42 };
43
44 /*
45   called when a epoll call fails
46 */
47 static void epoll_panic(struct epoll_event_context *epoll_ev, const char *reason)
48 {
49         tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
50                  "%s (%s) - calling abort()\n", reason, strerror(errno));
51         abort();
52 }
53
54 /*
55   map from TEVENT_FD_* to EPOLLIN/EPOLLOUT
56 */
57 static uint32_t epoll_map_flags(uint16_t flags)
58 {
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;
63 }
64
65 /*
66  free the epoll fd
67 */
68 static int epoll_ctx_destructor(struct epoll_event_context *epoll_ev)
69 {
70         close(epoll_ev->epoll_fd);
71         epoll_ev->epoll_fd = -1;
72         return 0;
73 }
74
75 /*
76  init the epoll fd
77 */
78 static int epoll_init_ctx(struct epoll_event_context *epoll_ev)
79 {
80         epoll_ev->epoll_fd = epoll_create(64);
81         epoll_ev->pid = getpid();
82         talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
83         if (epoll_ev->epoll_fd == -1) {
84                 return -1;
85         }
86         return 0;
87 }
88
89 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde);
90
91 /*
92   reopen the epoll handle when our pid changes
93   see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an 
94   demonstration of why this is needed
95  */
96 static void epoll_check_reopen(struct epoll_event_context *epoll_ev)
97 {
98         struct tevent_fd *fde;
99
100         if (epoll_ev->pid == getpid()) {
101                 return;
102         }
103
104         close(epoll_ev->epoll_fd);
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 recreate epoll handle after fork\n");
109                 return;
110         }
111         epoll_ev->pid = getpid();
112         for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) {
113                 epoll_add_event(epoll_ev, fde);
114         }
115 }
116
117 #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT      (1<<0)
118 #define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR   (1<<1)
119 #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR      (1<<2)
120
121 /*
122  add the epoll event to the given fd_event
123 */
124 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
125 {
126         struct epoll_event event;
127
128         if (epoll_ev->epoll_fd == -1) return;
129
130         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
131
132         /* if we don't want events yet, don't add an epoll_event */
133         if (fde->flags == 0) return;
134
135         ZERO_STRUCT(event);
136         event.events = epoll_map_flags(fde->flags);
137         event.data.ptr = fde;
138         if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
139                 epoll_panic(epoll_ev, "EPOLL_CTL_ADD failed");
140         }
141         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
142
143         /* only if we want to read we want to tell the event handler about errors */
144         if (fde->flags & TEVENT_FD_READ) {
145                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
146         }
147 }
148
149 /*
150  delete the epoll event for given fd_event
151 */
152 static void epoll_del_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
153 {
154         struct epoll_event event;
155
156         if (epoll_ev->epoll_fd == -1) return;
157
158         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
159
160         /* if there's no epoll_event, we don't need to delete it */
161         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
162
163         ZERO_STRUCT(event);
164         event.events = epoll_map_flags(fde->flags);
165         event.data.ptr = fde;
166         if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event) != 0) {
167                 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
168                              "epoll_del_event failed! probable early close bug (%s)\n",
169                              strerror(errno));
170         }
171         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
172 }
173
174 /*
175  change the epoll event to the given fd_event
176 */
177 static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
178 {
179         struct epoll_event event;
180         if (epoll_ev->epoll_fd == -1) return;
181
182         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
183
184         ZERO_STRUCT(event);
185         event.events = epoll_map_flags(fde->flags);
186         event.data.ptr = fde;
187         if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
188                 epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed");
189         }
190
191         /* only if we want to read we want to tell the event handler about errors */
192         if (fde->flags & TEVENT_FD_READ) {
193                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
194         }
195 }
196
197 static void epoll_change_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
198 {
199         bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
200         bool want_read = (fde->flags & TEVENT_FD_READ);
201         bool want_write= (fde->flags & TEVENT_FD_WRITE);
202
203         if (epoll_ev->epoll_fd == -1) return;
204
205         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
206
207         /* there's already an event */
208         if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
209                 if (want_read || (want_write && !got_error)) {
210                         epoll_mod_event(epoll_ev, fde);
211                         return;
212                 }
213                 /* 
214                  * if we want to match the select behavior, we need to remove the epoll_event
215                  * when the caller isn't interested in events.
216                  *
217                  * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
218                  */
219                 epoll_del_event(epoll_ev, fde);
220                 return;
221         }
222
223         /* there's no epoll_event attached to the fde */
224         if (want_read || (want_write && !got_error)) {
225                 epoll_add_event(epoll_ev, fde);
226                 return;
227         }
228 }
229
230 /*
231   event loop handling using epoll
232 */
233 static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp)
234 {
235         int ret, i;
236 #define MAXEVENTS 1
237         struct epoll_event events[MAXEVENTS];
238         int timeout = -1;
239
240         if (epoll_ev->epoll_fd == -1) return -1;
241
242         if (tvalp) {
243                 /* it's better to trigger timed events a bit later than to early */
244                 timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
245         }
246
247         if (epoll_ev->ev->signal_events &&
248             tevent_common_check_signal(epoll_ev->ev)) {
249                 return 0;
250         }
251
252         ret = epoll_wait(epoll_ev->epoll_fd, events, MAXEVENTS, timeout);
253
254         if (ret == -1 && errno == EINTR && epoll_ev->ev->signal_events) {
255                 if (tevent_common_check_signal(epoll_ev->ev)) {
256                         return 0;
257                 }
258         }
259
260         if (ret == -1 && errno != EINTR) {
261                 epoll_panic(epoll_ev, "epoll_wait() failed");
262                 return -1;
263         }
264
265         if (ret == 0 && tvalp) {
266                 /* we don't care about a possible delay here */
267                 tevent_common_loop_timer_delay(epoll_ev->ev);
268                 return 0;
269         }
270
271         for (i=0;i<ret;i++) {
272                 struct tevent_fd *fde = talloc_get_type(events[i].data.ptr, 
273                                                        struct tevent_fd);
274                 uint16_t flags = 0;
275
276                 if (fde == NULL) {
277                         epoll_panic(epoll_ev, "epoll_wait() gave bad data");
278                         return -1;
279                 }
280                 if (events[i].events & (EPOLLHUP|EPOLLERR)) {
281                         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR;
282                         /*
283                          * if we only wait for TEVENT_FD_WRITE, we should not tell the
284                          * event handler about it, and remove the epoll_event,
285                          * as we only report errors when waiting for read events,
286                          * to match the select() behavior
287                          */
288                         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
289                                 epoll_del_event(epoll_ev, fde);
290                                 continue;
291                         }
292                         flags |= TEVENT_FD_READ;
293                 }
294                 if (events[i].events & EPOLLIN) flags |= TEVENT_FD_READ;
295                 if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE;
296                 if (flags) {
297                         fde->handler(epoll_ev->ev, fde, flags, fde->private_data);
298                         break;
299                 }
300         }
301
302         return 0;
303 }
304
305 /*
306   create a epoll_event_context structure.
307 */
308 static int epoll_event_context_init(struct tevent_context *ev)
309 {
310         int ret;
311         struct epoll_event_context *epoll_ev;
312
313         epoll_ev = talloc_zero(ev, struct epoll_event_context);
314         if (!epoll_ev) return -1;
315         epoll_ev->ev = ev;
316         epoll_ev->epoll_fd = -1;
317
318         ret = epoll_init_ctx(epoll_ev);
319         if (ret != 0) {
320                 talloc_free(epoll_ev);
321                 return ret;
322         }
323
324         ev->additional_data = epoll_ev;
325         return 0;
326 }
327
328 /*
329   destroy an fd_event
330 */
331 static int epoll_event_fd_destructor(struct tevent_fd *fde)
332 {
333         struct tevent_context *ev = fde->event_ctx;
334         struct epoll_event_context *epoll_ev = NULL;
335
336         if (ev) {
337                 epoll_ev = talloc_get_type(ev->additional_data,
338                                            struct epoll_event_context);
339
340                 epoll_check_reopen(epoll_ev);
341
342                 epoll_del_event(epoll_ev, fde);
343         }
344
345         return tevent_common_fd_destructor(fde);
346 }
347
348 /*
349   add a fd based event
350   return NULL on failure (memory allocation error)
351 */
352 static struct tevent_fd *epoll_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
353                                             int fd, uint16_t flags,
354                                             tevent_fd_handler_t handler,
355                                             void *private_data,
356                                             const char *handler_name,
357                                             const char *location)
358 {
359         struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
360                                                            struct epoll_event_context);
361         struct tevent_fd *fde;
362
363         epoll_check_reopen(epoll_ev);
364
365         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
366                                    handler, private_data,
367                                    handler_name, location);
368         if (!fde) return NULL;
369
370         talloc_set_destructor(fde, epoll_event_fd_destructor);
371
372         epoll_add_event(epoll_ev, fde);
373
374         return fde;
375 }
376
377 /*
378   set the fd event flags
379 */
380 static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
381 {
382         struct tevent_context *ev;
383         struct epoll_event_context *epoll_ev;
384
385         if (fde->flags == flags) return;
386
387         ev = fde->event_ctx;
388         epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context);
389
390         fde->flags = flags;
391
392         epoll_check_reopen(epoll_ev);
393
394         epoll_change_event(epoll_ev, fde);
395 }
396
397 /*
398   do a single event loop using the events defined in ev 
399 */
400 static int epoll_event_loop_once(struct tevent_context *ev, const char *location)
401 {
402         struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
403                                                            struct epoll_event_context);
404         struct timeval tval;
405
406         if (ev->signal_events &&
407             tevent_common_check_signal(ev)) {
408                 return 0;
409         }
410
411         if (ev->immediate_events &&
412             tevent_common_loop_immediate(ev)) {
413                 return 0;
414         }
415
416         tval = tevent_common_loop_timer_delay(ev);
417         if (tevent_timeval_is_zero(&tval)) {
418                 return 0;
419         }
420
421         epoll_check_reopen(epoll_ev);
422
423         return epoll_event_loop(epoll_ev, &tval);
424 }
425
426 static const struct tevent_ops epoll_event_ops = {
427         .context_init           = epoll_event_context_init,
428         .add_fd                 = epoll_event_add_fd,
429         .set_fd_close_fn        = tevent_common_fd_set_close_fn,
430         .get_fd_flags           = tevent_common_fd_get_flags,
431         .set_fd_flags           = epoll_event_set_fd_flags,
432         .add_timer              = tevent_common_add_timer,
433         .schedule_immediate     = tevent_common_schedule_immediate,
434         .add_signal             = tevent_common_add_signal,
435         .loop_once              = epoll_event_loop_once,
436         .loop_wait              = tevent_common_loop_wait,
437 };
438
439 _PRIVATE_ bool tevent_epoll_init(void)
440 {
441         return tevent_register_backend("epoll", &epoll_event_ops);
442 }