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