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