]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
simplify io_library_init*
[ngircd-alex.git] / src / ngircd / io.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * Please read the file COPYING, README and AUTHORS for more information.
7  *
8  * I/O abstraction interface.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  *
11  */
12
13 #include "portab.h"
14
15 static char UNUSED id[] = "$Id: io.c,v 1.19 2006/09/16 16:47:27 fw Exp $";
16
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24
25 #include "array.h"
26 #include "io.h"
27 #include "log.h"
28
29 /* Enables extra debug messages in event add/delete/callback code. */
30 /* #define DEBUG_IO */
31
32 typedef struct {
33  void (*callback)(int, short);
34  short what;
35 } io_event;
36
37 #define INIT_IOEVENT    { NULL, -1, 0, NULL }
38 #define IO_ERROR        4
39
40 #ifdef HAVE_EPOLL_CREATE
41 #define IO_USE_EPOLL    1
42 #else
43 # ifdef HAVE_KQUEUE
44 #define IO_USE_KQUEUE   1
45 # else
46 #   ifdef HAVE_POLL
47 #define IO_USE_POLL     1
48 #   else
49 #define IO_USE_SELECT   1
50 #   endif /* HAVE_POLL */
51 # endif /* HAVE_KQUEUE */
52 #endif /* HAVE_EPOLL_CREATE */
53
54 static bool library_initialized;
55
56 #ifdef IO_USE_EPOLL
57 #include <sys/epoll.h>
58
59 static int io_masterfd;
60 static bool io_event_change_epoll(int fd, short what, const int action);
61 static int io_dispatch_epoll(struct timeval *tv);
62 #endif
63
64 #ifdef IO_USE_KQUEUE
65 #include <sys/types.h>
66 #include <sys/event.h>
67 static array io_evcache;
68 static int io_masterfd;
69
70 static int io_dispatch_kqueue(struct timeval *tv);
71 static bool io_event_change_kqueue(int, short, const int action);
72 #endif
73
74 #ifdef IO_USE_POLL
75 #include <poll.h>
76 static array pollfds;
77 static int poll_maxfd;
78
79 static bool io_event_change_poll(int fd, short what);
80 #endif
81
82 #ifdef IO_USE_SELECT
83 #include "defines.h"    /* for conn.h */
84 #include "conn.h"       /* for CONN_IDX (needed by resolve.h) */
85 #include "resolve.h"    /* for RES_STAT (needed by conf.h) */
86 #include "conf.h"       /* for Conf_MaxConnections */
87
88 static fd_set readers;
89 static fd_set writers;
90 static int select_maxfd;                /* the select() interface sucks badly */
91 static int io_dispatch_select(struct timeval *tv);
92 #endif
93
94 static array io_events;
95
96 static void io_docallback PARAMS((int fd, short what));
97
98 static io_event *
99 io_event_get(int fd)
100 {
101         io_event *i;
102
103         assert(fd >= 0);
104
105         i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
106
107         assert(i != NULL);
108
109         return i;
110 }
111
112
113 #ifdef IO_USE_POLL
114 static void
115 io_library_init_poll(unsigned int eventsize)
116 {
117         struct pollfd *p;
118         array_init(&pollfds);
119         poll_maxfd = 0;
120         Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
121             eventsize);
122         p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
123         if (p) {
124                 unsigned i;
125                 p = array_start(&pollfds);
126                 for (i = 0; i < eventsize; i++)
127                         p[i].fd = -1;
128
129                 library_initialized = true;
130         }
131 }
132 #endif
133
134
135 #ifdef IO_USE_SELECT
136 static void
137 io_library_init_select(unsigned int eventsize)
138 {
139         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
140             eventsize);
141         FD_ZERO(&readers);
142         FD_ZERO(&writers);
143 #ifdef FD_SETSIZE
144         if (Conf_MaxConnections >= (int)FD_SETSIZE) {
145                 Log(LOG_WARNING,
146                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
147                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
148
149                 Conf_MaxConnections = FD_SETSIZE - 1;
150         }
151 #endif /* FD_SETSIZE */
152         library_initialized = true;
153 }
154 #endif /* SELECT */
155
156
157 #ifdef IO_USE_EPOLL
158 static void
159 io_library_init_epoll(unsigned int eventsize)
160 {
161         int ecreate_hint = (int)eventsize;
162         if (ecreate_hint <= 0)
163                 ecreate_hint = 128;
164         io_masterfd = epoll_create(ecreate_hint);
165         Log(LOG_INFO,
166             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
167             ecreate_hint, eventsize, io_masterfd);
168         if (io_masterfd >= 0)
169                 library_initialized = true;
170 }
171 #endif
172
173
174 #ifdef IO_USE_KQUEUE
175 static void
176 io_library_init_kqueue(unsigned int eventsize)
177 {
178         io_masterfd = kqueue();
179
180         Log(LOG_INFO,
181             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
182             eventsize, io_masterfd);
183         if (io_masterfd >= 0)
184                 library_initialized = true;
185 }
186 #endif
187
188
189 bool
190 io_library_init(unsigned int eventsize)
191 {
192         if (library_initialized)
193                 return true;
194 #ifdef IO_USE_SELECT
195 #ifndef FD_SETSIZE
196         Log(LOG_WARNING,
197             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
198 #else
199         if (eventsize >= FD_SETSIZE)
200                 eventsize = FD_SETSIZE - 1;
201 #endif /* FD_SETSIZE */
202 #endif /* IO_USE_SELECT */
203         if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
204                 eventsize = 0;
205 #ifdef IO_USE_EPOLL
206         io_library_init_epoll(eventsize);
207 #endif
208 #ifdef IO_USE_KQUEUE
209         io_library_init_kqueue(eventsize);
210 #endif
211 #ifdef IO_USE_POLL
212         io_library_init_poll(eventsize);
213 #endif
214 #ifdef IO_USE_SELECT
215         io_library_init_select(eventsize);
216 #endif
217         return library_initialized;
218 }
219
220
221 void
222 io_library_shutdown(void)
223 {
224 #ifdef IO_USE_SELECT
225         FD_ZERO(&readers);
226         FD_ZERO(&writers);
227 #endif
228 #ifdef IO_USE_EPOLL
229         close(io_masterfd);
230         io_masterfd = -1;
231 #endif
232 #ifdef IO_USE_KQUEUE
233         close(io_masterfd);
234         io_masterfd = -1;
235         array_free(&io_evcache);
236 #endif
237         library_initialized = false;
238 }
239
240
241 bool
242 io_event_setcb(int fd, void (*cbfunc) (int, short))
243 {
244         io_event *i = io_event_get(fd);
245         if (!i)
246                 return false;
247
248         i->callback = cbfunc;
249         return true;
250 }
251
252
253 bool
254 io_event_create(int fd, short what, void (*cbfunc) (int, short))
255 {
256         bool ret;
257         io_event *i;
258
259         assert(fd >= 0);
260 #if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
261         if (fd >= FD_SETSIZE) {
262                 Log(LOG_ERR,
263                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
264                     fd, FD_SETSIZE);
265                 return false;
266         }
267 #endif
268         i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
269         if (!i) {
270                 Log(LOG_WARNING,
271                     "array_alloc failed: could not allocate space for %d io_event structures",
272                     fd);
273                 return false;
274         }
275
276         i->callback = cbfunc;
277         i->what = 0;
278 #ifdef IO_USE_POLL
279         ret = io_event_change_poll(fd, what);
280 #endif
281 #ifdef IO_USE_EPOLL
282         ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
283 #endif
284 #ifdef IO_USE_KQUEUE
285         ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
286 #endif
287 #ifdef IO_USE_SELECT
288         ret = io_event_add(fd, what);
289 #endif
290         if (ret) i->what = what;
291         return ret;
292 }
293
294
295 #ifdef IO_USE_POLL
296 static bool
297 io_event_change_poll(int fd, short what)
298 {
299         struct pollfd *p;
300         short events = 0;
301
302         if (what & IO_WANTREAD)
303                 events = POLLIN | POLLPRI;
304         if (what & IO_WANTWRITE)
305                 events |= POLLOUT;
306
307         p = array_alloc(&pollfds, sizeof *p, fd);
308         if (p) {
309                 p->events = events;
310                 p->fd = fd;
311                 if (fd > poll_maxfd)
312                         poll_maxfd = fd;
313         }
314         return p != NULL;
315 }
316 #endif
317
318 #ifdef IO_USE_EPOLL
319 static bool
320 io_event_change_epoll(int fd, short what, const int action)
321 {
322         struct epoll_event ev = { 0, {0} };
323         ev.data.fd = fd;
324
325         if (what & IO_WANTREAD)
326                 ev.events = EPOLLIN | EPOLLPRI;
327         if (what & IO_WANTWRITE)
328                 ev.events |= EPOLLOUT;
329
330         return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
331 }
332 #endif
333
334 #ifdef IO_USE_KQUEUE
335 static bool
336 io_event_kqueue_commit_cache(void)
337 {
338         struct kevent *events;
339         bool ret;
340         int len = (int) array_length(&io_evcache, sizeof (struct kevent));
341
342         if (!len) /* nothing to do */
343                 return true;
344
345         assert(len>0);
346
347         if (len < 0) {
348                 array_free(&io_evcache);
349                 return false;
350         }
351
352         events = array_start(&io_evcache);
353
354         assert(events != NULL);
355
356         ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
357         if (ret)
358                 array_trunc(&io_evcache);
359         return ret;
360 }
361
362
363 static bool
364 io_event_change_kqueue(int fd, short what, const int action)
365 {
366         struct kevent kev;
367         bool ret = true;
368
369         if (what & IO_WANTREAD) {
370                 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
371                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
372                 if (!ret)
373                         ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
374         }
375
376         if (ret && (what & IO_WANTWRITE)) {
377                 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
378                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
379                 if (!ret)
380                         ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
381         }
382
383         if (array_length(&io_evcache, sizeof kev) >= 100)
384                 io_event_kqueue_commit_cache();
385         return ret;
386 }
387 #endif
388
389
390 bool
391 io_event_add(int fd, short what)
392 {
393         io_event *i = io_event_get(fd);
394
395         if (!i) return false;
396         if (i->what == what) return true;
397 #ifdef DEBUG_IO
398         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
399 #endif
400         i->what |= what;
401 #ifdef IO_USE_EPOLL
402         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
403 #endif
404
405 #ifdef IO_USE_KQUEUE
406         return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
407 #endif
408 #ifdef IO_USE_POLL
409         return io_event_change_poll(fd, i->what);
410 #endif
411 #ifdef IO_USE_SELECT
412         if (fd > select_maxfd)
413                 select_maxfd = fd;
414
415         if (what & IO_WANTREAD)
416                 FD_SET(fd, &readers);
417         if (what & IO_WANTWRITE)
418                 FD_SET(fd, &writers);
419
420         return true;
421 #endif
422 }
423
424
425 bool
426 io_setnonblock(int fd)
427 {
428         int flags = fcntl(fd, F_GETFL);
429         if (flags == -1)
430                 return false;
431
432 #ifndef O_NONBLOCK
433 #define O_NONBLOCK O_NDELAY
434 #endif
435         flags |= O_NONBLOCK;
436
437         return fcntl(fd, F_SETFL, flags) == 0;
438 }
439
440
441 #ifdef IO_USE_POLL
442 static void
443 io_close_poll(int fd)
444 {
445         struct pollfd *p;
446         p = array_get(&pollfds, sizeof *p, fd);
447         if (!p) return;
448
449         p->fd = -1;
450         if (fd == poll_maxfd) {
451                 while (poll_maxfd > 0) {
452                         --poll_maxfd;
453                         p = array_get(&pollfds, sizeof *p, poll_maxfd);
454                         if (p && p->fd >= 0)
455                                 break;
456                 }
457         }
458 }
459 #else
460 static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
461 #endif
462
463
464 #ifdef IO_USE_SELECT
465 static void
466 io_close_select(int fd)
467 {
468         io_event *i;
469         FD_CLR(fd, &writers);
470         FD_CLR(fd, &readers);
471
472         i = io_event_get(fd);
473         if (!i) return;
474
475         if (fd == select_maxfd) {
476                 while (select_maxfd>0) {
477                         --select_maxfd; /* find largest fd */
478                         i = io_event_get(select_maxfd);
479                         if (i && i->callback) break;
480                 }
481         }
482 }
483 #else
484 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
485 #endif
486
487
488 bool
489 io_close(int fd)
490 {
491         io_event *i;
492
493         i = io_event_get(fd);
494 #ifdef IO_USE_KQUEUE
495         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
496                 io_event_kqueue_commit_cache();
497
498         /* both kqueue and epoll remove fd from all sets automatically on the last close
499          * of the descriptor. since we don't know if this is the last close we'll have
500          * to remove the set explicitly. */
501         if (i) {
502                 io_event_change_kqueue(fd, i->what, EV_DELETE);
503                 io_event_kqueue_commit_cache();
504         }
505 #endif
506
507         io_close_poll(fd);
508         io_close_select(fd);
509
510 #ifdef IO_USE_EPOLL
511         io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
512 #endif
513         if (i) {
514                 i->callback = NULL;
515                 i->what = 0;
516         }
517         return close(fd) == 0;
518 }
519
520
521 bool
522 io_event_del(int fd, short what)
523 {
524         io_event *i = io_event_get(fd);
525 #ifdef DEBUG_IO
526         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
527 #endif
528         if (!i) return false;
529
530         i->what &= ~what;
531
532 #ifdef IO_USE_POLL
533         return io_event_change_poll(fd, i->what);
534 #endif
535 #ifdef IO_USE_EPOLL
536         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
537 #endif
538
539 #ifdef IO_USE_KQUEUE
540         return io_event_change_kqueue(fd, what, EV_DISABLE);
541 #endif
542 #ifdef IO_USE_SELECT
543         if (what & IO_WANTWRITE)
544                 FD_CLR(fd, &writers);
545
546         if (what & IO_WANTREAD)
547                 FD_CLR(fd, &readers);
548
549         return true;
550 #endif
551 }
552
553
554 #ifdef IO_USE_SELECT
555 static int
556 io_dispatch_select(struct timeval *tv)
557 {
558         fd_set readers_tmp = readers;
559         fd_set writers_tmp = writers;
560         short what;
561         int ret, i;
562         int fds_ready;
563         ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
564         if (ret <= 0)
565                 return ret;
566
567         fds_ready = ret;
568
569         for (i = 0; i <= select_maxfd; i++) {
570                 what = 0;
571                 if (FD_ISSET(i, &readers_tmp)) {
572                         what = IO_WANTREAD;
573                         fds_ready--;
574                 }
575
576                 if (FD_ISSET(i, &writers_tmp)) {
577                         what |= IO_WANTWRITE;
578                         fds_ready--;
579                 }
580                 if (what)
581                         io_docallback(i, what);
582                 if (fds_ready <= 0)
583                         break;
584         }
585
586         return ret;
587 }
588 #endif
589
590
591 #ifdef IO_USE_POLL
592 static int
593 io_dispatch_poll(struct timeval *tv)
594 {
595         time_t sec = tv->tv_sec * 1000;
596         int i, ret, timeout = tv->tv_usec + sec;
597         int fds_ready;
598         short what;
599         struct pollfd *p = array_start(&pollfds);
600
601         if (timeout < 0)
602                 timeout = 1000;
603
604         ret = poll(p, poll_maxfd + 1, timeout);
605         if (ret <= 0)
606                 return ret;
607
608         fds_ready = ret;
609         for (i=0; i <= poll_maxfd; i++) {
610                 what = 0;
611                 if (p[i].revents & (POLLIN|POLLPRI))
612                         what = IO_WANTREAD;
613
614                 if (p[i].revents & POLLOUT)
615                         what |= IO_WANTWRITE;
616
617                 if (p[i].revents && !what) {
618                         /* other flag is set, probably POLLERR */
619                         what = IO_ERROR;
620                 }
621                 if (what) {
622                         fds_ready--;
623                         io_docallback(i, what);
624                 }
625                 if (fds_ready <= 0)
626                         break;
627         }
628
629         return ret;
630 }
631 #endif
632
633
634 #ifdef IO_USE_EPOLL
635 static int
636 io_dispatch_epoll(struct timeval *tv)
637 {
638         time_t sec = tv->tv_sec * 1000;
639         int i, total = 0, ret, timeout = tv->tv_usec + sec;
640         struct epoll_event epoll_ev[100];
641         short type;
642
643         if (timeout < 0)
644                 timeout = 1000;
645
646         do {
647                 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
648                 total += ret;
649                 if (ret <= 0)
650                         return total;
651
652                 for (i = 0; i < ret; i++) {
653                         type = 0;
654                         if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
655                                 type = IO_ERROR;
656
657                         if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
658                                 type |= IO_WANTREAD;
659
660                         if (epoll_ev[i].events & EPOLLOUT)
661                                 type |= IO_WANTWRITE;
662
663                         io_docallback(epoll_ev[i].data.fd, type);
664                 }
665
666                 timeout = 0;
667         } while (ret == 100);
668
669         return total;
670 }
671 #endif
672
673
674 #ifdef IO_USE_KQUEUE
675 static int
676 io_dispatch_kqueue(struct timeval *tv)
677 {
678         int i, total = 0, ret;
679         struct kevent kev[100];
680         struct kevent *newevents;
681         struct timespec ts;
682         int newevents_len;
683         ts.tv_sec = tv->tv_sec;
684         ts.tv_nsec = tv->tv_usec * 1000;
685
686         do {
687                 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
688                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
689                 assert(newevents_len >= 0);
690                 if (newevents_len < 0)
691                         newevents_len = 0;
692 #ifdef DEBUG
693                 if (newevents_len)
694                         assert(newevents != NULL);
695 #endif
696                 ret = kevent(io_masterfd, newevents, newevents_len, kev,
697                              100, &ts);
698                 if ((newevents_len>0) && ret != -1)
699                         array_trunc(&io_evcache);
700
701                 total += ret;
702                 if (ret <= 0)
703                         return total;
704
705                 for (i = 0; i < ret; i++) {
706                         if (kev[i].flags & EV_EOF) {
707 #ifdef DEBUG
708                                 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
709                                         kev[i].filter, kev[i].ident);
710 #endif
711                                 io_docallback((int)kev[i].ident, IO_ERROR);
712                                 continue;
713                         }
714
715                         switch (kev[i].filter) {
716                                 case EVFILT_READ:
717                                         io_docallback((int)kev[i].ident, IO_WANTREAD);
718                                         break;
719                                 case EVFILT_WRITE:
720                                         io_docallback((int)kev[i].ident, IO_WANTWRITE);
721                                         break;
722                                 default:
723 #ifdef DEBUG
724                                         LogDebug("Unknown kev.filter number %d for fd %d",
725                                                 kev[i].filter, kev[i].ident); /* Fall through */
726 #endif
727                                 case EV_ERROR:
728                                         io_docallback((int)kev[i].ident, IO_ERROR);
729                                         break;
730                         }
731                 }
732                 ts.tv_sec = 0;
733                 ts.tv_nsec = 0;
734         } while (ret == 100);
735
736         return total;
737 }
738 #endif
739
740
741 int
742 io_dispatch(struct timeval *tv)
743 {
744 #ifdef IO_USE_SELECT
745         return io_dispatch_select(tv);
746 #endif
747 #ifdef IO_USE_KQUEUE
748         return io_dispatch_kqueue(tv);
749 #endif
750 #ifdef IO_USE_POLL
751         return io_dispatch_poll(tv);
752 #endif
753 #ifdef IO_USE_EPOLL
754         return io_dispatch_epoll(tv);
755 #endif
756 }
757
758
759 /* call the callback function inside the struct matching fd */
760 static void
761 io_docallback(int fd, short what)
762 {
763         io_event *i;
764 #ifdef DEBUG_IO
765         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
766 #endif
767         i = io_event_get(fd);
768
769         if (i->callback) {      /* callback might be NULL if a previous callback function
770                                    called io_close on this fd */
771                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
772         }
773         /* if error indicator is set, we return the event(s) that were registered */
774 }