lib/events: system/network.h isn't needed
[tprouty/samba.git] / source / lib / events / events_standard.c
1 /* 
2    Unix SMB/CIFS implementation.
3    main select loop and event handling
4    Copyright (C) Andrew Tridgell        2003-2005
5    Copyright (C) Stefan Metzmacher      2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22   This is SAMBA's default event loop code
23
24   - we try to use epoll if configure detected support for it
25     otherwise we use select()
26   - if epoll is broken on the system or the kernel doesn't support it
27     at runtime we fallback to select()
28 */
29
30 #if _SAMBA_BUILD_
31 #include "includes.h"
32 #include "lib/util/dlinklist.h"
33 #else
34 #include "replace.h"
35 #include "events_util.h"
36 #endif
37 #include "system/filesys.h"
38 #include "system/select.h" /* needed for HAVE_EVENTS_EPOLL */
39 #include "events.h"
40 #include "events_internal.h"
41
42 struct std_event_context {
43         /* a pointer back to the generic event_context */
44         struct event_context *ev;
45
46         /* list of filedescriptor events */
47         struct fd_event *fd_events;
48
49         /* the maximum file descriptor number in fd_events */
50         int maxfd;
51
52         /* information for exiting from the event loop */
53         int exit_code;
54
55         /* this is changed by the destructors for the fd event
56            type. It is used to detect event destruction by event
57            handlers, which means the code that is calling the event
58            handler needs to assume that the linked list is no longer
59            valid
60         */
61         uint32_t destruction_count;
62
63         /* when using epoll this is the handle from epoll_create */
64         int epoll_fd;
65
66         /* our pid at the time the epoll_fd was created */
67         pid_t pid;
68 };
69
70 /* use epoll if it is available */
71 #if HAVE_EVENTS_EPOLL
72 /*
73   called when a epoll call fails, and we should fallback
74   to using select
75 */
76 static void epoll_fallback_to_select(struct std_event_context *std_ev, const char *reason)
77 {
78         DEBUG(0,("%s (%s) - falling back to select()\n", reason, strerror(errno)));
79         close(std_ev->epoll_fd);
80         std_ev->epoll_fd = -1;
81         talloc_set_destructor(std_ev, NULL);
82 }
83
84 /*
85   map from EVENT_FD_* to EPOLLIN/EPOLLOUT
86 */
87 static uint32_t epoll_map_flags(uint16_t flags)
88 {
89         uint32_t ret = 0;
90         if (flags & EVENT_FD_READ) ret |= (EPOLLIN | EPOLLERR | EPOLLHUP);
91         if (flags & EVENT_FD_WRITE) ret |= (EPOLLOUT | EPOLLERR | EPOLLHUP);
92         return ret;
93 }
94
95 /*
96  free the epoll fd
97 */
98 static int epoll_ctx_destructor(struct std_event_context *std_ev)
99 {
100         if (std_ev->epoll_fd != -1) {
101                 close(std_ev->epoll_fd);
102         }
103         std_ev->epoll_fd = -1;
104         return 0;
105 }
106
107 /*
108  init the epoll fd
109 */
110 static void epoll_init_ctx(struct std_event_context *std_ev)
111 {
112         std_ev->epoll_fd = epoll_create(64);
113         std_ev->pid = getpid();
114         talloc_set_destructor(std_ev, epoll_ctx_destructor);
115 }
116
117 static void epoll_add_event(struct std_event_context *std_ev, struct fd_event *fde);
118
119 /*
120   reopen the epoll handle when our pid changes
121   see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an 
122   demonstration of why this is needed
123  */
124 static void epoll_check_reopen(struct std_event_context *std_ev)
125 {
126         struct fd_event *fde;
127
128         if (std_ev->pid == getpid()) {
129                 return;
130         }
131
132         close(std_ev->epoll_fd);
133         std_ev->epoll_fd = epoll_create(64);
134         if (std_ev->epoll_fd == -1) {
135                 DEBUG(0,("Failed to recreate epoll handle after fork\n"));
136                 return;
137         }
138         std_ev->pid = getpid();
139         for (fde=std_ev->fd_events;fde;fde=fde->next) {
140                 epoll_add_event(std_ev, fde);
141         }
142 }
143
144 #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT      (1<<0)
145 #define EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR   (1<<1)
146 #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR      (1<<2)
147
148 /*
149  add the epoll event to the given fd_event
150 */
151 static void epoll_add_event(struct std_event_context *std_ev, struct fd_event *fde)
152 {
153         struct epoll_event event;
154         if (std_ev->epoll_fd == -1) return;
155
156         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
157
158         /* if we don't want events yet, don't add an epoll_event */
159         if (fde->flags == 0) return;
160
161         ZERO_STRUCT(event);
162         event.events = epoll_map_flags(fde->flags);
163         event.data.ptr = fde;
164         if (epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_ADD, fde->fd, &event) != 0) {
165                 epoll_fallback_to_select(std_ev, "EPOLL_CTL_ADD failed");
166         }
167         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
168
169         /* only if we want to read we want to tell the event handler about errors */
170         if (fde->flags & EVENT_FD_READ) {
171                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
172         }
173 }
174
175 /*
176  delete the epoll event for given fd_event
177 */
178 static void epoll_del_event(struct std_event_context *std_ev, struct fd_event *fde)
179 {
180         struct epoll_event event;
181         if (std_ev->epoll_fd == -1) return;
182
183         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
184
185         /* if there's no epoll_event, we don't need to delete it */
186         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT)) return;
187
188         ZERO_STRUCT(event);
189         event.events = epoll_map_flags(fde->flags);
190         event.data.ptr = fde;
191         epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_DEL, fde->fd, &event);
192         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT;
193 }
194
195 /*
196  change the epoll event to the given fd_event
197 */
198 static void epoll_mod_event(struct std_event_context *std_ev, struct fd_event *fde)
199 {
200         struct epoll_event event;
201         if (std_ev->epoll_fd == -1) return;
202
203         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
204
205         ZERO_STRUCT(event);
206         event.events = epoll_map_flags(fde->flags);
207         event.data.ptr = fde;
208         if (epoll_ctl(std_ev->epoll_fd, EPOLL_CTL_MOD, fde->fd, &event) != 0) {
209                 epoll_fallback_to_select(std_ev, "EPOLL_CTL_MOD failed");
210         }
211
212         /* only if we want to read we want to tell the event handler about errors */
213         if (fde->flags & EVENT_FD_READ) {
214                 fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
215         }
216 }
217
218 static void epoll_change_event(struct std_event_context *std_ev, struct fd_event *fde)
219 {
220         bool got_error = (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR);
221         bool want_read = (fde->flags & EVENT_FD_READ);
222         bool want_write= (fde->flags & EVENT_FD_WRITE);
223
224         if (std_ev->epoll_fd == -1) return;
225
226         fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR;
227
228         /* there's already an event */
229         if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) {
230                 if (want_read || (want_write && !got_error)) {
231                         epoll_mod_event(std_ev, fde);
232                         return;
233                 }
234                 /* 
235                  * if we want to match the select behavior, we need to remove the epoll_event
236                  * when the caller isn't interested in events.
237                  *
238                  * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them
239                  */
240                 epoll_del_event(std_ev, fde);
241                 return;
242         }
243
244         /* there's no epoll_event attached to the fde */
245         if (want_read || (want_write && !got_error)) {
246                 epoll_add_event(std_ev, fde);
247                 return;
248         }
249 }
250
251 /*
252   event loop handling using epoll
253 */
254 static int epoll_event_loop(struct std_event_context *std_ev, struct timeval *tvalp)
255 {
256         int ret, i;
257 #define MAXEVENTS 8
258         struct epoll_event events[MAXEVENTS];
259         uint32_t destruction_count = ++std_ev->destruction_count;
260         int timeout = -1;
261
262         if (std_ev->epoll_fd == -1) return -1;
263
264         if (tvalp) {
265                 /* it's better to trigger timed events a bit later than to early */
266                 timeout = ((tvalp->tv_usec+999) / 1000) + (tvalp->tv_sec*1000);
267         }
268
269         if (std_ev->ev->num_signal_handlers && 
270             common_event_check_signal(std_ev->ev)) {
271                 return 0;
272         }
273
274         ret = epoll_wait(std_ev->epoll_fd, events, MAXEVENTS, timeout);
275
276         if (ret == -1 && errno == EINTR && std_ev->ev->num_signal_handlers) {
277                 if (common_event_check_signal(std_ev->ev)) {
278                         return 0;
279                 }
280         }
281
282         if (ret == -1 && errno != EINTR) {
283                 epoll_fallback_to_select(std_ev, "epoll_wait() failed");
284                 return -1;
285         }
286
287         if (ret == 0 && tvalp) {
288                 /* we don't care about a possible delay here */
289                 common_event_loop_timer_delay(std_ev->ev);
290                 return 0;
291         }
292
293         for (i=0;i<ret;i++) {
294                 struct fd_event *fde = talloc_get_type(events[i].data.ptr, 
295                                                        struct fd_event);
296                 uint16_t flags = 0;
297
298                 if (fde == NULL) {
299                         epoll_fallback_to_select(std_ev, "epoll_wait() gave bad data");
300                         return -1;
301                 }
302                 if (events[i].events & (EPOLLHUP|EPOLLERR)) {
303                         fde->additional_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR;
304                         /*
305                          * if we only wait for EVENT_FD_WRITE, we should not tell the
306                          * event handler about it, and remove the epoll_event,
307                          * as we only report errors when waiting for read events,
308                          * to match the select() behavior
309                          */
310                         if (!(fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR)) {
311                                 epoll_del_event(std_ev, fde);
312                                 continue;
313                         }
314                         flags |= EVENT_FD_READ;
315                 }
316                 if (events[i].events & EPOLLIN) flags |= EVENT_FD_READ;
317                 if (events[i].events & EPOLLOUT) flags |= EVENT_FD_WRITE;
318                 if (flags) {
319                         fde->handler(std_ev->ev, fde, flags, fde->private_data);
320                         if (destruction_count != std_ev->destruction_count) {
321                                 break;
322                         }
323                 }
324         }
325
326         return 0;
327 }
328 #else
329 #define epoll_init_ctx(std_ev) 
330 #define epoll_add_event(std_ev,fde)
331 #define epoll_del_event(std_ev,fde)
332 #define epoll_change_event(std_ev,fde)
333 #define epoll_event_loop(std_ev,tvalp) (-1)
334 #define epoll_check_reopen(std_ev)
335 #endif
336
337 /*
338   create a std_event_context structure.
339 */
340 static int std_event_context_init(struct event_context *ev)
341 {
342         struct std_event_context *std_ev;
343
344         std_ev = talloc_zero(ev, struct std_event_context);
345         if (!std_ev) return -1;
346         std_ev->ev = ev;
347         std_ev->epoll_fd = -1;
348
349         epoll_init_ctx(std_ev);
350
351         ev->additional_data = std_ev;
352         return 0;
353 }
354
355 /*
356   recalculate the maxfd
357 */
358 static void calc_maxfd(struct std_event_context *std_ev)
359 {
360         struct fd_event *fde;
361
362         std_ev->maxfd = 0;
363         for (fde = std_ev->fd_events; fde; fde = fde->next) {
364                 if (fde->fd > std_ev->maxfd) {
365                         std_ev->maxfd = fde->fd;
366                 }
367         }
368 }
369
370
371 /* to mark the ev->maxfd invalid
372  * this means we need to recalculate it
373  */
374 #define EVENT_INVALID_MAXFD (-1)
375
376 /*
377   destroy an fd_event
378 */
379 static int std_event_fd_destructor(struct fd_event *fde)
380 {
381         struct event_context *ev = fde->event_ctx;
382         struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
383                                                            struct std_event_context);
384
385         epoll_check_reopen(std_ev);
386
387         if (std_ev->maxfd == fde->fd) {
388                 std_ev->maxfd = EVENT_INVALID_MAXFD;
389         }
390
391         DLIST_REMOVE(std_ev->fd_events, fde);
392         std_ev->destruction_count++;
393
394         epoll_del_event(std_ev, fde);
395
396         if (fde->flags & EVENT_FD_AUTOCLOSE) {
397                 close(fde->fd);
398                 fde->fd = -1;
399         }
400
401         return 0;
402 }
403
404 /*
405   add a fd based event
406   return NULL on failure (memory allocation error)
407 */
408 static struct fd_event *std_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
409                                          int fd, uint16_t flags,
410                                          event_fd_handler_t handler,
411                                          void *private_data)
412 {
413         struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
414                                                            struct std_event_context);
415         struct fd_event *fde;
416
417         epoll_check_reopen(std_ev);
418
419         fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
420         if (!fde) return NULL;
421
422         fde->event_ctx          = ev;
423         fde->fd                 = fd;
424         fde->flags              = flags;
425         fde->handler            = handler;
426         fde->private_data       = private_data;
427         fde->additional_flags   = 0;
428         fde->additional_data    = NULL;
429
430         DLIST_ADD(std_ev->fd_events, fde);
431         if ((std_ev->maxfd != EVENT_INVALID_MAXFD)
432             && (fde->fd > std_ev->maxfd)) {
433                 std_ev->maxfd = fde->fd;
434         }
435         talloc_set_destructor(fde, std_event_fd_destructor);
436
437         epoll_add_event(std_ev, fde);
438
439         return fde;
440 }
441
442
443 /*
444   return the fd event flags
445 */
446 static uint16_t std_event_get_fd_flags(struct fd_event *fde)
447 {
448         return fde->flags;
449 }
450
451 /*
452   set the fd event flags
453 */
454 static void std_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
455 {
456         struct event_context *ev;
457         struct std_event_context *std_ev;
458
459         if (fde->flags == flags) return;
460
461         ev = fde->event_ctx;
462         std_ev = talloc_get_type(ev->additional_data, struct std_event_context);
463
464         fde->flags = flags;
465
466         epoll_check_reopen(std_ev);
467
468         epoll_change_event(std_ev, fde);
469 }
470
471 /*
472   event loop handling using select()
473 */
474 static int std_event_loop_select(struct std_event_context *std_ev, struct timeval *tvalp)
475 {
476         fd_set r_fds, w_fds;
477         struct fd_event *fde;
478         int selrtn;
479         uint32_t destruction_count = ++std_ev->destruction_count;
480
481         /* we maybe need to recalculate the maxfd */
482         if (std_ev->maxfd == EVENT_INVALID_MAXFD) {
483                 calc_maxfd(std_ev);
484         }
485
486         FD_ZERO(&r_fds);
487         FD_ZERO(&w_fds);
488
489         /* setup any fd events */
490         for (fde = std_ev->fd_events; fde; fde = fde->next) {
491                 if (fde->flags & EVENT_FD_READ) {
492                         FD_SET(fde->fd, &r_fds);
493                 }
494                 if (fde->flags & EVENT_FD_WRITE) {
495                         FD_SET(fde->fd, &w_fds);
496                 }
497         }
498
499         if (std_ev->ev->num_signal_handlers && 
500             common_event_check_signal(std_ev->ev)) {
501                 return 0;
502         }
503
504         selrtn = select(std_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
505
506         if (selrtn == -1 && errno == EINTR && 
507             std_ev->ev->num_signal_handlers) {
508                 common_event_check_signal(std_ev->ev);
509                 return 0;
510         }
511
512         if (selrtn == -1 && errno == EBADF) {
513                 /* the socket is dead! this should never
514                    happen as the socket should have first been
515                    made readable and that should have removed
516                    the event, so this must be a bug. This is a
517                    fatal error. */
518                 DEBUG(0,("ERROR: EBADF on std_event_loop_once\n"));
519                 std_ev->exit_code = EBADF;
520                 return -1;
521         }
522
523         if (selrtn == 0 && tvalp) {
524                 /* we don't care about a possible delay here */
525                 common_event_loop_timer_delay(std_ev->ev);
526                 return 0;
527         }
528
529         if (selrtn > 0) {
530                 /* at least one file descriptor is ready - check
531                    which ones and call the handler, being careful to allow
532                    the handler to remove itself when called */
533                 for (fde = std_ev->fd_events; fde; fde = fde->next) {
534                         uint16_t flags = 0;
535
536                         if (FD_ISSET(fde->fd, &r_fds)) flags |= EVENT_FD_READ;
537                         if (FD_ISSET(fde->fd, &w_fds)) flags |= EVENT_FD_WRITE;
538                         if (flags) {
539                                 fde->handler(std_ev->ev, fde, flags, fde->private_data);
540                                 if (destruction_count != std_ev->destruction_count) {
541                                         break;
542                                 }
543                         }
544                 }
545         }
546
547         return 0;
548 }               
549
550 /*
551   do a single event loop using the events defined in ev 
552 */
553 static int std_event_loop_once(struct event_context *ev)
554 {
555         struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
556                                                            struct std_event_context);
557         struct timeval tval;
558
559         tval = common_event_loop_timer_delay(ev);
560         if (timeval_is_zero(&tval)) {
561                 return 0;
562         }
563
564         epoll_check_reopen(std_ev);
565
566         if (epoll_event_loop(std_ev, &tval) == 0) {
567                 return 0;
568         }
569
570         return std_event_loop_select(std_ev, &tval);
571 }
572
573 /*
574   return on failure or (with 0) if all fd events are removed
575 */
576 static int std_event_loop_wait(struct event_context *ev)
577 {
578         struct std_event_context *std_ev = talloc_get_type(ev->additional_data,
579                                                            struct std_event_context);
580         std_ev->exit_code = 0;
581
582         while (std_ev->fd_events && std_ev->exit_code == 0) {
583                 if (std_event_loop_once(ev) != 0) {
584                         break;
585                 }
586         }
587
588         return std_ev->exit_code;
589 }
590
591 static const struct event_ops std_event_ops = {
592         .context_init   = std_event_context_init,
593         .add_fd         = std_event_add_fd,
594         .get_fd_flags   = std_event_get_fd_flags,
595         .set_fd_flags   = std_event_set_fd_flags,
596         .add_timed      = common_event_add_timed,
597         .add_signal     = common_event_add_signal,
598         .loop_once      = std_event_loop_once,
599         .loop_wait      = std_event_loop_wait,
600 };
601
602
603 bool events_standard_init(void)
604 {
605         return event_register_backend("standard", &std_event_ops);
606 }
607
608 #if _SAMBA_BUILD_
609 _PUBLIC_ NTSTATUS s4_events_standard_init(void)
610 {
611         if (!events_standard_init()) {
612                 return NT_STATUS_INTERNAL_ERROR;
613         }
614         return NT_STATUS_OK;
615 }
616 #endif