tevent: Ensure we return after every call to epoll_panic().
[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         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;
85         }
86
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");
90         }
91
92         epoll_ev->pid = getpid();
93         talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
94
95         return 0;
96 }
97
98 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde);
99
100 /*
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
104  */
105 static void epoll_check_reopen(struct epoll_event_context *epoll_ev)
106 {
107         struct tevent_fd *fde;
108
109         if (epoll_ev->pid == getpid()) {
110                 return;
111         }
112
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;
119         }
120
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");
124         }
125
126         epoll_ev->pid = getpid();
127         for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) {
128                 epoll_add_event(epoll_ev, fde);
129         }
130 }
131
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)
135
136 /*
137  add the epoll event to the given fd_event
138 */
139 static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
140 {
141         struct epoll_event event;
142
143         if (epoll_ev->epoll_fd == -1) return;
144
145         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
146
147         /* if we don't want events yet, don't add an epoll_event */
148         if (fde->flags == 0) return;
149
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");
155                 return;
156         }
157         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
158
159         /* only if we want to read we want to tell the event handler about errors */
160         if (fde->flags & TEVENT_FD_READ) {
161                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
162         }
163 }
164
165 /*
166  delete the epoll event for given fd_event
167 */
168 static void epoll_del_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
169 {
170         struct epoll_event event;
171
172         if (epoll_ev->epoll_fd == -1) return;
173
174         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
175
176         /* if there's no epoll_event, we don't need to delete it */
177         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
178
179         ZERO_STRUCT(event);
180         event.events = epoll_map_flags(fde->flags);
181         event.data.ptr = fde;
182         if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event) != 0) {
183                 tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
184                              "epoll_del_event failed! probable early close bug (%s)\n",
185                              strerror(errno));
186         }
187         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
188 }
189
190 /*
191  change the epoll event to the given fd_event
192 */
193 static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
194 {
195         struct epoll_event event;
196         if (epoll_ev->epoll_fd == -1) return;
197
198         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
199
200         ZERO_STRUCT(event);
201         event.events = epoll_map_flags(fde->flags);
202         event.data.ptr = fde;
203         if (epoll_ctl(epoll_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
204                 epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed");
205                 return;
206         }
207
208         /* only if we want to read we want to tell the event handler about errors */
209         if (fde->flags & TEVENT_FD_READ) {
210                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
211         }
212 }
213
214 static void epoll_change_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde)
215 {
216         bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
217         bool want_read = (fde->flags & TEVENT_FD_READ);
218         bool want_write= (fde->flags & TEVENT_FD_WRITE);
219
220         if (epoll_ev->epoll_fd == -1) return;
221
222         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
223
224         /* there's already an event */
225         if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
226                 if (want_read || (want_write && !got_error)) {
227                         epoll_mod_event(epoll_ev, fde);
228                         return;
229                 }
230                 /* 
231                  * if we want to match the select behavior, we need to remove the epoll_event
232                  * when the caller isn't interested in events.
233                  *
234                  * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
235                  */
236                 epoll_del_event(epoll_ev, fde);
237                 return;
238         }
239
240         /* there's no epoll_event attached to the fde */
241         if (want_read || (want_write && !got_error)) {
242                 epoll_add_event(epoll_ev, fde);
243                 return;
244         }
245 }
246
247 /*
248   event loop handling using epoll
249 */
250 static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp)
251 {
252         int ret, i;
253 #define MAXEVENTS 1
254         struct epoll_event events[MAXEVENTS];
255         int timeout = -1;
256
257         if (epoll_ev->epoll_fd == -1) return -1;
258
259         if (tvalp) {
260                 /* it's better to trigger timed events a bit later than to early */
261                 timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
262         }
263
264         if (epoll_ev->ev->signal_events &&
265             tevent_common_check_signal(epoll_ev->ev)) {
266                 return 0;
267         }
268
269         tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_BEFORE_WAIT);
270         ret = epoll_wait(epoll_ev->epoll_fd, events, MAXEVENTS, timeout);
271         tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_AFTER_WAIT);
272
273         if (ret == -1 && errno == EINTR && epoll_ev->ev->signal_events) {
274                 if (tevent_common_check_signal(epoll_ev->ev)) {
275                         return 0;
276                 }
277         }
278
279         if (ret == -1 && errno != EINTR) {
280                 epoll_panic(epoll_ev, "epoll_wait() failed");
281                 return -1;
282         }
283
284         if (ret == 0 && tvalp) {
285                 /* we don't care about a possible delay here */
286                 tevent_common_loop_timer_delay(epoll_ev->ev);
287                 return 0;
288         }
289
290         for (i=0;i<ret;i++) {
291                 struct tevent_fd *fde = talloc_get_type(events[i].data.ptr, 
292                                                        struct tevent_fd);
293                 uint16_t flags = 0;
294
295                 if (fde == NULL) {
296                         epoll_panic(epoll_ev, "epoll_wait() gave bad data");
297                         return -1;
298                 }
299                 if (events[i].events & (EPOLLHUP|EPOLLERR)) {
300                         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR;
301                         /*
302                          * if we only wait for TEVENT_FD_WRITE, we should not tell the
303                          * event handler about it, and remove the epoll_event,
304                          * as we only report errors when waiting for read events,
305                          * to match the select() behavior
306                          */
307                         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
308                                 epoll_del_event(epoll_ev, fde);
309                                 continue;
310                         }
311                         flags |= TEVENT_FD_READ;
312                 }
313                 if (events[i].events & EPOLLIN) flags |= TEVENT_FD_READ;
314                 if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE;
315                 if (flags) {
316                         fde->handler(epoll_ev->ev, fde, flags, fde->private_data);
317                         break;
318                 }
319         }
320
321         return 0;
322 }
323
324 /*
325   create a epoll_event_context structure.
326 */
327 static int epoll_event_context_init(struct tevent_context *ev)
328 {
329         int ret;
330         struct epoll_event_context *epoll_ev;
331
332         epoll_ev = talloc_zero(ev, struct epoll_event_context);
333         if (!epoll_ev) return -1;
334         epoll_ev->ev = ev;
335         epoll_ev->epoll_fd = -1;
336
337         ret = epoll_init_ctx(epoll_ev);
338         if (ret != 0) {
339                 talloc_free(epoll_ev);
340                 return ret;
341         }
342
343         ev->additional_data = epoll_ev;
344         return 0;
345 }
346
347 /*
348   destroy an fd_event
349 */
350 static int epoll_event_fd_destructor(struct tevent_fd *fde)
351 {
352         struct tevent_context *ev = fde->event_ctx;
353         struct epoll_event_context *epoll_ev = NULL;
354
355         if (ev) {
356                 epoll_ev = talloc_get_type(ev->additional_data,
357                                            struct epoll_event_context);
358
359                 epoll_check_reopen(epoll_ev);
360
361                 epoll_del_event(epoll_ev, fde);
362         }
363
364         return tevent_common_fd_destructor(fde);
365 }
366
367 /*
368   add a fd based event
369   return NULL on failure (memory allocation error)
370 */
371 static struct tevent_fd *epoll_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
372                                             int fd, uint16_t flags,
373                                             tevent_fd_handler_t handler,
374                                             void *private_data,
375                                             const char *handler_name,
376                                             const char *location)
377 {
378         struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
379                                                            struct epoll_event_context);
380         struct tevent_fd *fde;
381
382         epoll_check_reopen(epoll_ev);
383
384         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
385                                    handler, private_data,
386                                    handler_name, location);
387         if (!fde) return NULL;
388
389         talloc_set_destructor(fde, epoll_event_fd_destructor);
390
391         epoll_add_event(epoll_ev, fde);
392
393         return fde;
394 }
395
396 /*
397   set the fd event flags
398 */
399 static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
400 {
401         struct tevent_context *ev;
402         struct epoll_event_context *epoll_ev;
403
404         if (fde->flags == flags) return;
405
406         ev = fde->event_ctx;
407         epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context);
408
409         fde->flags = flags;
410
411         epoll_check_reopen(epoll_ev);
412
413         epoll_change_event(epoll_ev, fde);
414 }
415
416 /*
417   do a single event loop using the events defined in ev 
418 */
419 static int epoll_event_loop_once(struct tevent_context *ev, const char *location)
420 {
421         struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data,
422                                                            struct epoll_event_context);
423         struct timeval tval;
424
425         if (ev->signal_events &&
426             tevent_common_check_signal(ev)) {
427                 return 0;
428         }
429
430         if (ev->immediate_events &&
431             tevent_common_loop_immediate(ev)) {
432                 return 0;
433         }
434
435         tval = tevent_common_loop_timer_delay(ev);
436         if (tevent_timeval_is_zero(&tval)) {
437                 return 0;
438         }
439
440         epoll_check_reopen(epoll_ev);
441
442         return epoll_event_loop(epoll_ev, &tval);
443 }
444
445 static const struct tevent_ops epoll_event_ops = {
446         .context_init           = epoll_event_context_init,
447         .add_fd                 = epoll_event_add_fd,
448         .set_fd_close_fn        = tevent_common_fd_set_close_fn,
449         .get_fd_flags           = tevent_common_fd_get_flags,
450         .set_fd_flags           = epoll_event_set_fd_flags,
451         .add_timer              = tevent_common_add_timer,
452         .schedule_immediate     = tevent_common_schedule_immediate,
453         .add_signal             = tevent_common_add_signal,
454         .loop_once              = epoll_event_loop_once,
455         .loop_wait              = tevent_common_loop_wait,
456 };
457
458 _PRIVATE_ bool tevent_epoll_init(void)
459 {
460         return tevent_register_backend("epoll", &epoll_event_ops);
461 }