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