]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
Removed unused variable "ret" when using the select() API.
[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.16 2006/07/23 23:11:44 alex 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 #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE)
105         bool ret;
106 #endif
107 #ifdef IO_USE_EPOLL
108         int ecreate_hint = (int)eventsize;
109         if (ecreate_hint <= 0)
110                 ecreate_hint = 128;
111 #endif
112         if (library_initialized)
113                 return true;
114
115 #ifdef IO_USE_SELECT
116 #ifdef FD_SETSIZE
117         if (eventsize >= FD_SETSIZE)
118                 eventsize = FD_SETSIZE - 1;
119 #endif
120 #endif
121         if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
122                 eventsize = 0;
123
124 #ifdef IO_USE_EPOLL
125         io_masterfd = epoll_create(ecreate_hint);
126         Log(LOG_INFO,
127             "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
128             ecreate_hint, eventsize, io_masterfd);
129         ret = io_masterfd >= 0;
130         if (ret) library_initialized = true;
131
132         return ret;
133 #endif
134 #ifdef IO_USE_SELECT
135         Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
136             eventsize);
137         FD_ZERO(&readers);
138         FD_ZERO(&writers);
139 #ifdef FD_SETSIZE
140         if (Conf_MaxConnections >= (int)FD_SETSIZE) {
141                 Log(LOG_WARNING,
142                     "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
143                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
144
145                 Conf_MaxConnections = FD_SETSIZE - 1;
146         }
147 #else
148         Log(LOG_WARNING,
149             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
150 #endif /* FD_SETSIZE */
151         library_initialized = true;
152         return true;
153 #endif /* SELECT */
154 #ifdef IO_USE_KQUEUE
155         io_masterfd = kqueue();
156
157         Log(LOG_INFO,
158             "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
159             eventsize, io_masterfd);
160         ret = io_masterfd >= 0;
161         if (ret) library_initialized = true;
162
163         return ret;
164 #endif
165 }
166
167
168 void
169 io_library_shutdown(void)
170 {
171 #ifdef IO_USE_SELECT
172         FD_ZERO(&readers);
173         FD_ZERO(&writers);
174 #else
175         close(io_masterfd);     /* kqueue, epoll */
176         io_masterfd = -1;
177 #endif
178 #ifdef IO_USE_KQUEUE
179         array_free(&io_evcache);
180 #endif
181         library_initialized = false;
182 }
183
184
185 bool
186 io_event_setcb(int fd, void (*cbfunc) (int, short))
187 {
188         io_event *i = io_event_get(fd);
189         if (!i)
190                 return false;
191
192         i->callback = cbfunc;
193         return true;
194 }
195
196
197 bool
198 io_event_create(int fd, short what, void (*cbfunc) (int, short))
199 {
200         bool ret;
201         io_event *i;
202
203         assert(fd >= 0);
204
205 #ifdef IO_USE_SELECT
206 #ifdef FD_SETSIZE
207         if (fd >= FD_SETSIZE) {
208                 Log(LOG_ERR,
209                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
210                     fd, FD_SETSIZE);
211                 return false;
212         }
213 #endif                          /* FD_SETSIZE */
214 #endif                          /* IO_USE_SELECT */
215
216         i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
217         if (!i) {
218                 Log(LOG_WARNING,
219                     "array_alloc failed: could not allocate space for %d io_event structures",
220                     fd);
221                 return false;
222         }
223
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->callback) 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                 i->callback = NULL;
395                 i->what = 0;
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(fd, &writers);
422
423         if (what & IO_WANTREAD)
424                 FD_CLR(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 }