]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
remove fd from io_event structure
[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.15 2006/07/12 19:27:12 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
48 #endif
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 bool
102 io_library_init(unsigned int eventsize)
103 {
104         bool ret;
105 #ifdef IO_USE_EPOLL
106         int ecreate_hint = (int)eventsize;
107         if (ecreate_hint <= 0)
108                 ecreate_hint = 128;
109 #endif
110         if (library_initialized)
111                 return true;
112
113 #ifdef IO_USE_SELECT
114 #ifdef FD_SETSIZE
115         if (eventsize >= FD_SETSIZE)
116                 eventsize = FD_SETSIZE - 1;
117 #endif
118 #endif
119         if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
120                 eventsize = 0;
121
122 #ifdef IO_USE_EPOLL
123         io_masterfd = epoll_create(ecreate_hint);
124         Log(LOG_INFO,
125             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
126             ecreate_hint, eventsize, io_masterfd);
127         ret = io_masterfd >= 0;
128         if (ret) library_initialized = true;
129
130         return ret;
131 #endif
132 #ifdef IO_USE_SELECT
133         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
134             eventsize);
135         FD_ZERO(&readers);
136         FD_ZERO(&writers);
137 #ifdef FD_SETSIZE
138         if (Conf_MaxConnections >= (int)FD_SETSIZE) {
139                 Log(LOG_WARNING,
140                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
141                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
142
143                 Conf_MaxConnections = FD_SETSIZE - 1;
144         }
145 #else
146         Log(LOG_WARNING,
147             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
148 #endif /* FD_SETSIZE */
149         library_initialized = true;
150         return true;
151 #endif /* SELECT */
152 #ifdef IO_USE_KQUEUE
153         io_masterfd = kqueue();
154
155         Log(LOG_INFO,
156             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
157             eventsize, io_masterfd);
158         ret = io_masterfd >= 0;
159         if (ret) library_initialized = true;
160
161         return ret;
162 #endif
163 }
164
165
166 void
167 io_library_shutdown(void)
168 {
169 #ifdef IO_USE_SELECT
170         FD_ZERO(&readers);
171         FD_ZERO(&writers);
172 #else
173         close(io_masterfd);     /* kqueue, epoll */
174         io_masterfd = -1;
175 #endif
176 #ifdef IO_USE_KQUEUE
177         array_free(&io_evcache);
178 #endif
179         library_initialized = false;
180 }
181
182
183 bool
184 io_event_setcb(int fd, void (*cbfunc) (int, short))
185 {
186         io_event *i = io_event_get(fd);
187         if (!i)
188                 return false;
189
190         i->callback = cbfunc;
191         return true;
192 }
193
194
195 bool
196 io_event_create(int fd, short what, void (*cbfunc) (int, short))
197 {
198         bool ret;
199         io_event *i;
200
201         assert(fd >= 0);
202
203 #ifdef IO_USE_SELECT
204 #ifdef FD_SETSIZE
205         if (fd >= FD_SETSIZE) {
206                 Log(LOG_ERR,
207                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
208                     fd, FD_SETSIZE);
209                 return false;
210         }
211 #endif                          /* FD_SETSIZE */
212 #endif                          /* IO_USE_SELECT */
213
214         i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
215         if (!i) {
216                 Log(LOG_WARNING,
217                     "array_alloc failed: could not allocate space for %d io_event structures",
218                     fd);
219                 return false;
220         }
221
222         i->callback = cbfunc;
223         i->what = 0;
224 #ifdef IO_USE_EPOLL
225         ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
226 #endif
227 #ifdef IO_USE_KQUEUE
228         ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
229 #endif
230 #ifdef IO_USE_SELECT
231         ret = io_event_add(fd, what);
232 #endif
233         if (ret) i->what = what;
234         return ret;
235 }
236
237
238 #ifdef IO_USE_EPOLL
239 static bool
240 io_event_change_epoll(int fd, short what, const int action)
241 {
242         struct epoll_event ev = { 0, {0} };
243         ev.data.fd = fd;
244
245         if (what & IO_WANTREAD)
246                 ev.events = EPOLLIN | EPOLLPRI;
247         if (what & IO_WANTWRITE)
248                 ev.events |= EPOLLOUT;
249
250         return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
251 }
252 #endif
253
254 #ifdef IO_USE_KQUEUE
255 static bool
256 io_event_kqueue_commit_cache(void)
257 {
258         struct kevent *events;
259         bool ret;
260         int len = (int) array_length(&io_evcache, sizeof (struct kevent));
261  
262         if (!len) /* nothing to do */
263                 return true;
264
265         assert(len>0);
266
267         if (len < 0) {
268                 array_free(&io_evcache);
269                 return false;
270         }
271
272         events = array_start(&io_evcache);
273
274         assert(events != NULL);
275
276         ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
277         if (ret)
278                 array_trunc(&io_evcache);
279         return ret;
280 }
281
282
283 static bool
284 io_event_change_kqueue(int fd, short what, const int action)
285 {
286         struct kevent kev;
287         bool ret = true;
288
289         if (what & IO_WANTREAD) {
290                 EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
291                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
292                 if (!ret)
293                         ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
294         }       
295
296         if (ret && (what & IO_WANTWRITE)) {
297                 EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
298                 ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
299                 if (!ret)
300                         ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
301         }
302
303         if (array_length(&io_evcache, sizeof kev) >= 100)
304                 io_event_kqueue_commit_cache();
305         return ret;
306 }
307 #endif
308
309
310 bool
311 io_event_add(int fd, short what)
312 {
313         io_event *i = io_event_get(fd);
314
315         if (!i) return false;
316         if (i->what == what) return true;
317 #ifdef DEBUG_IO
318         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
319 #endif
320         i->what |= what;
321 #ifdef IO_USE_EPOLL
322         return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
323 #endif
324
325 #ifdef IO_USE_KQUEUE
326         return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
327 #endif
328
329 #ifdef IO_USE_SELECT
330         if (fd > select_maxfd)
331                 select_maxfd = fd;
332
333         if (what & IO_WANTREAD)
334                 FD_SET(fd, &readers);
335         if (what & IO_WANTWRITE)
336                 FD_SET(fd, &writers);
337
338         return true;
339 #endif
340 }
341
342
343 bool
344 io_setnonblock(int fd)
345 {
346         int flags = fcntl(fd, F_GETFL);
347         if (flags == -1)
348                 return false;
349
350 #ifndef O_NONBLOCK
351 #define O_NONBLOCK O_NDELAY
352 #endif
353         flags |= O_NONBLOCK;
354
355         return fcntl(fd, F_SETFL, flags) == 0;
356 }
357
358
359 bool
360 io_close(int fd)
361 {
362         io_event *i;
363 #ifdef IO_USE_SELECT
364         FD_CLR(fd, &writers);
365         FD_CLR(fd, &readers);
366
367         if (fd == select_maxfd) {
368                 while (select_maxfd>0) {
369                         --select_maxfd; /* find largest fd */  
370                         i = io_event_get(select_maxfd);
371                         if (i && i->callback) break;
372                 }       
373         }       
374 #endif
375         i = io_event_get(fd);
376 #ifdef IO_USE_KQUEUE
377         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
378                 io_event_kqueue_commit_cache();
379
380         /* both kqueue and epoll remove fd from all sets automatically on the last close
381          * of the descriptor. since we don't know if this is the last close we'll have
382          * to remove the set explicitly. */
383         if (i) {
384                 io_event_change_kqueue(fd, i->what, EV_DELETE);
385                 io_event_kqueue_commit_cache();
386         }       
387 #endif
388 #ifdef IO_USE_EPOLL
389         io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
390 #endif
391         if (i) {
392                 i->callback = NULL;
393                 i->what = 0;
394         }
395         return close(fd) == 0;
396 }
397
398
399 bool
400 io_event_del(int fd, short what)
401 {
402         io_event *i = io_event_get(fd);
403 #ifdef DEBUG_IO
404         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
405 #endif
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(fd, &writers);
420
421         if (what & IO_WANTREAD)
422                 FD_CLR(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 != NULL);
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                         if (kev[i].flags & EV_EOF) {
539 #ifdef DEBUG
540                                 LogDebug("kev.flag has EV_EOF set, setting IO_ERROR",
541                                         kev[i].filter, kev[i].ident);
542 #endif                          
543                                 io_docallback((int)kev[i].ident, IO_ERROR);
544                                 continue;
545                         }       
546
547                         switch (kev[i].filter) {
548                                 case EVFILT_READ:
549                                         io_docallback((int)kev[i].ident, IO_WANTREAD);
550                                         break;
551                                 case EVFILT_WRITE:
552                                         io_docallback((int)kev[i].ident, IO_WANTWRITE);
553                                         break;
554                                 default:
555 #ifdef DEBUG
556                                         LogDebug("Unknown kev.filter number %d for fd %d",
557                                                 kev[i].filter, kev[i].ident); /* Fall through */
558 #endif
559                                 case EV_ERROR:
560                                         io_docallback((int)kev[i].ident, IO_ERROR);
561                                         break;
562                         }
563                 }
564                 ts.tv_sec = 0;
565                 ts.tv_nsec = 0;
566         } while (ret == 100);
567
568         return total;
569 }
570 #endif
571
572
573 int
574 io_dispatch(struct timeval *tv)
575 {
576 #ifdef IO_USE_SELECT
577         return io_dispatch_select(tv);
578 #endif
579 #ifdef IO_USE_KQUEUE
580         return io_dispatch_kqueue(tv);
581 #endif
582 #ifdef IO_USE_EPOLL
583         return io_dispatch_epoll(tv);
584 #endif
585 }
586
587
588 /* call the callback function inside the struct matching fd */
589 static void
590 io_docallback(int fd, short what)
591 {
592         io_event *i;
593 #ifdef DEBUG_IO
594         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
595 #endif
596         i = io_event_get(fd);
597
598         if (i->callback) {      /* callback might be NULL if a previous callback function 
599                                    called io_close on this fd */
600                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
601         }       
602         /* if error indicator is set, we return the event(s) that were registered */
603 }