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