]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
cleanup
[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.17 2006/09/16 14:49:26 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 #define IO_USE_SELECT   1
47 # endif /* HAVE_KQUEUE */
48 #endif /* HAVE_EPOLL_CREATE */
49
50 static bool library_initialized;
51
52 #ifdef IO_USE_EPOLL
53 #include <sys/epoll.h>
54
55 static int io_masterfd;
56 static bool io_event_change_epoll(int fd, short what, const int action);
57 static int io_dispatch_epoll(struct timeval *tv);
58 #endif
59
60 #ifdef IO_USE_KQUEUE
61 #include <sys/types.h>
62 #include <sys/event.h>
63 static array io_evcache;
64 static int io_masterfd;
65
66 static int io_dispatch_kqueue(struct timeval *tv);
67 static bool io_event_change_kqueue(int, short, const int action);
68 #endif
69
70 #ifdef IO_USE_SELECT
71 #include "defines.h"    /* for conn.h */
72 #include "conn.h"       /* for CONN_IDX (needed by resolve.h) */
73 #include "resolve.h"    /* for RES_STAT (needed by conf.h) */
74 #include "conf.h"       /* for Conf_MaxConnections */
75
76 static fd_set readers;
77 static fd_set writers;
78 static int select_maxfd;                /* the select() interface sucks badly */
79 static int io_dispatch_select(struct timeval *tv);
80 #endif
81
82 static array io_events;
83
84 static void io_docallback PARAMS((int fd, short what));
85
86 static io_event *
87 io_event_get(int fd)
88 {
89         io_event *i;
90
91         assert(fd >= 0);
92
93         i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
94
95         assert(i != NULL);
96
97         return i;
98 }
99
100
101 #ifdef IO_USE_SELECT
102 static bool
103 io_library_init_select(unsigned int eventsize)
104 {
105         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
106             eventsize);
107         FD_ZERO(&readers);
108         FD_ZERO(&writers);
109 #ifdef FD_SETSIZE
110         if (Conf_MaxConnections >= (int)FD_SETSIZE) {
111                 Log(LOG_WARNING,
112                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
113                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
114
115                 Conf_MaxConnections = FD_SETSIZE - 1;
116         }
117 #endif /* FD_SETSIZE */
118         library_initialized = true;
119         return true;
120 }
121 #endif /* SELECT */
122
123
124 #ifdef IO_USE_EPOLL
125 static bool
126 io_library_init_epoll(unsigned int eventsize)
127 {
128         bool ret;
129         int ecreate_hint = (int)eventsize;
130         if (ecreate_hint <= 0)
131                 ecreate_hint = 128;
132         io_masterfd = epoll_create(ecreate_hint);
133         Log(LOG_INFO,
134             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
135             ecreate_hint, eventsize, io_masterfd);
136         ret = io_masterfd >= 0;
137         if (ret) library_initialized = true;
138
139         return ret;
140 }
141 #endif
142
143
144 #ifdef IO_USE_KQUEUE
145 static bool
146 io_library_init_kqueue(unsigned int eventsize)
147 {
148         bool ret;
149         io_masterfd = kqueue();
150
151         Log(LOG_INFO,
152             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
153             eventsize, io_masterfd);
154         ret = io_masterfd >= 0;
155         if (ret) library_initialized = true;
156         return ret;
157 }
158 #endif
159
160
161 bool
162 io_library_init(unsigned int eventsize)
163 {
164         if (library_initialized)
165                 return true;
166 #ifdef IO_USE_SELECT
167 #ifndef FD_SETSIZE
168         Log(LOG_WARNING,
169             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
170 #else
171         if (eventsize >= FD_SETSIZE)
172                 eventsize = FD_SETSIZE - 1;
173 #endif /* FD_SETSIZE */
174 #endif /* IO_USE_SELECT */
175         if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
176                 eventsize = 0;
177 #ifdef IO_USE_EPOLL
178         return io_library_init_epoll(eventsize);
179 #endif
180 #ifdef IO_USE_KQUEUE
181         return io_library_init_kqueue(eventsize);
182 #endif
183 #ifdef IO_USE_SELECT
184         return io_library_init_select(eventsize);
185 #endif
186 }
187
188
189 void
190 io_library_shutdown(void)
191 {
192 #ifdef IO_USE_SELECT
193         FD_ZERO(&readers);
194         FD_ZERO(&writers);
195 #endif
196 #ifdef IO_USE_EPOLL
197         close(io_masterfd);
198         io_masterfd = -1;
199 #endif
200 #ifdef IO_USE_KQUEUE
201         close(io_masterfd);
202         io_masterfd = -1;
203         array_free(&io_evcache);
204 #endif
205         library_initialized = false;
206 }
207
208
209 bool
210 io_event_setcb(int fd, void (*cbfunc) (int, short))
211 {
212         io_event *i = io_event_get(fd);
213         if (!i)
214                 return false;
215
216         i->callback = cbfunc;
217         return true;
218 }
219
220
221 bool
222 io_event_create(int fd, short what, void (*cbfunc) (int, short))
223 {
224         bool ret;
225         io_event *i;
226
227         assert(fd >= 0);
228 #if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
229         if (fd >= FD_SETSIZE) {
230                 Log(LOG_ERR,
231                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
232                     fd, FD_SETSIZE);
233                 return false;
234         }
235 #endif
236         i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
237         if (!i) {
238                 Log(LOG_WARNING,
239                     "array_alloc failed: could not allocate space for %d io_event structures",
240                     fd);
241                 return false;
242         }
243
244         i->callback = cbfunc;
245         i->what = 0;
246 #ifdef IO_USE_EPOLL
247         ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
248 #endif
249 #ifdef IO_USE_KQUEUE
250         ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
251 #endif
252 #ifdef IO_USE_SELECT
253         ret = io_event_add(fd, what);
254 #endif
255         if (ret) i->what = what;
256         return ret;
257 }
258
259
260 #ifdef IO_USE_EPOLL
261 static bool
262 io_event_change_epoll(int fd, short what, const int action)
263 {
264         struct epoll_event ev = { 0, {0} };
265         ev.data.fd = fd;
266
267         if (what & IO_WANTREAD)
268                 ev.events = EPOLLIN | EPOLLPRI;
269         if (what & IO_WANTWRITE)
270                 ev.events |= EPOLLOUT;
271
272         return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
273 }
274 #endif
275
276 #ifdef IO_USE_KQUEUE
277 static bool
278 io_event_kqueue_commit_cache(void)
279 {
280         struct kevent *events;
281         bool ret;
282         int len = (int) array_length(&io_evcache, sizeof (struct kevent));
283
284         if (!len) /* nothing to do */
285                 return true;
286
287         assert(len>0);
288
289         if (len < 0) {
290                 array_free(&io_evcache);
291                 return false;
292         }
293
294         events = array_start(&io_evcache);
295
296         assert(events != NULL);
297
298         ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
299         if (ret)
300                 array_trunc(&io_evcache);
301         return ret;
302 }
303
304
305 static bool
306 io_event_change_kqueue(int fd, short what, const int action)
307 {
308         struct kevent kev;
309         bool ret = true;
310
311         if (what & IO_WANTREAD) {
312                 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
313                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
314                 if (!ret)
315                         ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
316         }
317
318         if (ret && (what & IO_WANTWRITE)) {
319                 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
320                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
321                 if (!ret)
322                         ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
323         }
324
325         if (array_length(&io_evcache, sizeof kev) >= 100)
326                 io_event_kqueue_commit_cache();
327         return ret;
328 }
329 #endif
330
331
332 bool
333 io_event_add(int fd, short what)
334 {
335         io_event *i = io_event_get(fd);
336
337         if (!i) return false;
338         if (i->what == what) return true;
339 #ifdef DEBUG_IO
340         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
341 #endif
342         i->what |= what;
343 #ifdef IO_USE_EPOLL
344         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
345 #endif
346
347 #ifdef IO_USE_KQUEUE
348         return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
349 #endif
350 #ifdef IO_USE_SELECT
351         if (fd > select_maxfd)
352                 select_maxfd = fd;
353
354         if (what & IO_WANTREAD)
355                 FD_SET(fd, &readers);
356         if (what & IO_WANTWRITE)
357                 FD_SET(fd, &writers);
358
359         return true;
360 #endif
361 }
362
363
364 bool
365 io_setnonblock(int fd)
366 {
367         int flags = fcntl(fd, F_GETFL);
368         if (flags == -1)
369                 return false;
370
371 #ifndef O_NONBLOCK
372 #define O_NONBLOCK O_NDELAY
373 #endif
374         flags |= O_NONBLOCK;
375
376         return fcntl(fd, F_SETFL, flags) == 0;
377 }
378
379
380 #ifdef IO_USE_SELECT
381 static void
382 io_close_select(int fd)
383 {
384         io_event *i;
385         FD_CLR(fd, &writers);
386         FD_CLR(fd, &readers);
387
388         i = io_event_get(fd);
389         if (!i) return;
390
391         if (fd == select_maxfd) {
392                 while (select_maxfd>0) {
393                         --select_maxfd; /* find largest fd */
394                         i = io_event_get(select_maxfd);
395                         if (i && i->callback) break;
396                 }
397         }
398 }
399 #else
400 static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
401 #endif
402
403
404 bool
405 io_close(int fd)
406 {
407         io_event *i;
408
409         i = io_event_get(fd);
410 #ifdef IO_USE_KQUEUE
411         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
412                 io_event_kqueue_commit_cache();
413
414         /* both kqueue and epoll remove fd from all sets automatically on the last close
415          * of the descriptor. since we don't know if this is the last close we'll have
416          * to remove the set explicitly. */
417         if (i) {
418                 io_event_change_kqueue(fd, i->what, EV_DELETE);
419                 io_event_kqueue_commit_cache();
420         }
421 #endif
422
423         io_close_select(fd);
424
425 #ifdef IO_USE_EPOLL
426         io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
427 #endif
428         if (i) {
429                 i->callback = NULL;
430                 i->what = 0;
431         }
432         return close(fd) == 0;
433 }
434
435
436 bool
437 io_event_del(int fd, short what)
438 {
439         io_event *i = io_event_get(fd);
440 #ifdef DEBUG_IO
441         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
442 #endif
443         if (!i) return false;
444
445         i->what &= ~what;
446
447 #ifdef IO_USE_EPOLL
448         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
449 #endif
450
451 #ifdef IO_USE_KQUEUE
452         return io_event_change_kqueue(fd, what, EV_DISABLE);
453 #endif
454 #ifdef IO_USE_SELECT
455         if (what & IO_WANTWRITE)
456                 FD_CLR(fd, &writers);
457
458         if (what & IO_WANTREAD)
459                 FD_CLR(fd, &readers);
460
461         return true;
462 #endif
463 }
464
465
466 #ifdef IO_USE_SELECT
467 static int
468 io_dispatch_select(struct timeval *tv)
469 {
470         fd_set readers_tmp = readers;
471         fd_set writers_tmp = writers;
472         short what;
473         int ret, i;
474         int fds_ready;
475         ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
476         if (ret <= 0)
477                 return ret;
478
479         fds_ready = ret;
480
481         for (i = 0; i <= select_maxfd; i++) {
482                 what = 0;
483                 if (FD_ISSET(i, &readers_tmp)) {
484                         what = IO_WANTREAD;
485                         fds_ready--;
486                 }
487
488                 if (FD_ISSET(i, &writers_tmp)) {
489                         what |= IO_WANTWRITE;
490                         fds_ready--;
491                 }
492                 if (what)
493                         io_docallback(i, what);
494                 if (fds_ready <= 0)
495                         break;
496         }
497
498         return ret;
499 }
500 #endif
501
502
503 #ifdef IO_USE_EPOLL
504 static int
505 io_dispatch_epoll(struct timeval *tv)
506 {
507         time_t sec = tv->tv_sec * 1000;
508         int i, total = 0, ret, timeout = tv->tv_usec + sec;
509         struct epoll_event epoll_ev[100];
510         short type;
511
512         if (timeout < 0)
513                 timeout = 1000;
514
515         do {
516                 ret = epoll_wait(io_masterfd, epoll_ev, 100, timeout);
517                 total += ret;
518                 if (ret <= 0)
519                         return total;
520
521                 for (i = 0; i < ret; i++) {
522                         type = 0;
523                         if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
524                                 type = IO_ERROR;
525
526                         if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
527                                 type |= IO_WANTREAD;
528
529                         if (epoll_ev[i].events & EPOLLOUT)
530                                 type |= IO_WANTWRITE;
531
532                         io_docallback(epoll_ev[i].data.fd, type);
533                 }
534
535                 timeout = 0;
536         } while (ret == 100);
537
538         return total;
539 }
540 #endif
541
542
543 #ifdef IO_USE_KQUEUE
544 static int
545 io_dispatch_kqueue(struct timeval *tv)
546 {
547         int i, total = 0, ret;
548         struct kevent kev[100];
549         struct kevent *newevents;
550         struct timespec ts;
551         int newevents_len;
552         ts.tv_sec = tv->tv_sec;
553         ts.tv_nsec = tv->tv_usec * 1000;
554
555         do {
556                 newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
557                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
558                 assert(newevents_len >= 0);
559                 if (newevents_len < 0)
560                         newevents_len = 0;
561 #ifdef DEBUG
562                 if (newevents_len)
563                         assert(newevents != NULL);
564 #endif
565                 ret = kevent(io_masterfd, newevents, newevents_len, kev,
566                              100, &ts);
567                 if ((newevents_len>0) && ret != -1)
568                         array_trunc(&io_evcache);
569
570                 total += ret;
571                 if (ret <= 0)
572                         return total;
573
574                 for (i = 0; i < ret; i++) {
575                         if (kev[i].flags & EV_EOF) {
576 #ifdef DEBUG
577                                 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
578                                         kev[i].filter, kev[i].ident);
579 #endif
580                                 io_docallback((int)kev[i].ident, IO_ERROR);
581                                 continue;
582                         }
583
584                         switch (kev[i].filter) {
585                                 case EVFILT_READ:
586                                         io_docallback((int)kev[i].ident, IO_WANTREAD);
587                                         break;
588                                 case EVFILT_WRITE:
589                                         io_docallback((int)kev[i].ident, IO_WANTWRITE);
590                                         break;
591                                 default:
592 #ifdef DEBUG
593                                         LogDebug("Unknown kev.filter number %d for fd %d",
594                                                 kev[i].filter, kev[i].ident); /* Fall through */
595 #endif
596                                 case EV_ERROR:
597                                         io_docallback((int)kev[i].ident, IO_ERROR);
598                                         break;
599                         }
600                 }
601                 ts.tv_sec = 0;
602                 ts.tv_nsec = 0;
603         } while (ret == 100);
604
605         return total;
606 }
607 #endif
608
609
610 int
611 io_dispatch(struct timeval *tv)
612 {
613 #ifdef IO_USE_SELECT
614         return io_dispatch_select(tv);
615 #endif
616 #ifdef IO_USE_KQUEUE
617         return io_dispatch_kqueue(tv);
618 #endif
619 #ifdef IO_USE_EPOLL
620         return io_dispatch_epoll(tv);
621 #endif
622 }
623
624
625 /* call the callback function inside the struct matching fd */
626 static void
627 io_docallback(int fd, short what)
628 {
629         io_event *i;
630 #ifdef DEBUG_IO
631         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
632 #endif
633         i = io_event_get(fd);
634
635         if (i->callback) {      /* callback might be NULL if a previous callback function
636                                    called io_close on this fd */
637                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
638         }
639         /* if error indicator is set, we return the event(s) that were registered */
640 }