]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/io.c
removed unneeded return statement
[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.3 2005/07/12 20:44:13 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
50 #ifdef IO_USE_EPOLL
51 #include <sys/epoll.h>
52
53 static int io_masterfd;
54 static bool io_event_new_epoll(int fd, short what);
55 static bool io_event_change_epoll(int fd, short what);
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_add_kqueue(int, short);
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 #ifdef IO_USE_EPOLL
101         int ecreate_hint = (int)eventsize;
102         if (ecreate_hint <= 0)
103                 ecreate_hint = 128;
104 #endif
105
106 #ifdef IO_USE_SELECT
107 #ifdef FD_SETSIZE
108         if (eventsize >= FD_SETSIZE)
109                 eventsize = FD_SETSIZE - 1;
110 #endif
111 #endif
112         if (eventsize && !array_alloc(&io_events, sizeof(io_event), eventsize))
113                 eventsize = 0;
114
115 #ifdef IO_USE_EPOLL
116         io_masterfd = epoll_create(ecreate_hint);
117         Log(LOG_INFO,
118             "io subsystem: using epoll (hint size %d), initial io_event maxfd: %u, io_masterfd %d",
119             ecreate_hint, eventsize, io_masterfd);
120         return io_masterfd >= 0;
121 #endif
122 #ifdef IO_USE_SELECT
123         Log(LOG_INFO, "io subsystem: using select, initial io_event maxfd: %u",
124             eventsize);
125         FD_ZERO(&readers);
126         FD_ZERO(&writers);
127 #ifdef FD_SETSIZE
128         if (Conf_MaxConnections >= FD_SETSIZE) {
129                 Log(LOG_WARNING,
130                     "Conf_MaxConnections (%d) exceeds limit (%u), changed Conf_MaxConnections to %u",
131                     Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
132
133                 Conf_MaxConnections = FD_SETSIZE - 1;
134         }
135 #else
136         Log(LOG_WARNING,
137             "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform");
138 #endif
139         return true;
140 #endif
141 #ifdef IO_USE_KQUEUE
142         io_masterfd = kqueue();
143
144         Log(LOG_INFO,
145             "io subsystem: using kqueue, initial io_event maxfd: %u, io_masterfd %d",
146             eventsize, io_masterfd);
147         return io_masterfd >= 0;
148 #endif
149 }
150
151
152 void
153 io_library_shutdown(void)
154 {
155 #ifdef IO_USE_SELECT
156         FD_ZERO(&readers);
157         FD_ZERO(&writers);
158 #else
159         close(io_masterfd);     /* kqueue, epoll */
160         io_masterfd = -1;
161 #endif
162 #ifdef IO_USE_KQUEUE
163         array_free(&io_evcache);
164 #endif
165 }
166
167
168 bool
169 io_event_setcb(int fd, void (*cbfunc) (int, short))
170 {
171         io_event *i = io_event_get(fd);
172         if (!i)
173                 return false;
174
175         i->callback = cbfunc;
176         return true;
177 }
178
179
180 bool
181 io_event_create(int fd, short what, void (*cbfunc) (int, short))
182 {
183         io_event *i;
184
185         assert(fd >= 0);
186
187 #ifdef IO_USE_SELECT
188 #ifdef FD_SETSIZE
189         if (fd >= FD_SETSIZE) {
190                 Log(LOG_ERR,
191                     "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
192                     fd, FD_SETSIZE);
193                 return false;
194         }
195 #endif                          /* FD_SETSIZE */
196 #endif                          /* IO_USE_SELECT */
197
198         i = (io_event *) array_alloc(&io_events, sizeof(io_event), fd);
199         if (!i) {
200                 Log(LOG_WARNING,
201                     "array_alloc failed: could not allocate space for %d io_event structures",
202                     fd);
203                 return false;
204         }
205
206         i->fd = fd;
207         i->callback = cbfunc;
208 #ifdef IO_USE_EPOLL
209         i->what = what;
210         return io_event_new_epoll(fd, what);
211 #endif
212 #ifdef IO_USE_KQUEUE
213         i->what = what;
214         return io_event_add_kqueue(fd, what);
215 #endif
216 #ifdef IO_USE_SELECT
217         i->what = 0;
218         return io_event_add(fd, what);
219 #endif
220 }
221
222
223 #ifdef IO_USE_EPOLL
224 static bool
225 io_event_new_epoll(int fd, short what)
226 {
227         struct epoll_event ev = { 0, {0} };
228         ev.data.fd = fd;
229
230         if (what & IO_WANTREAD)
231                 ev.events = EPOLLIN | EPOLLPRI;
232         if (what & IO_WANTWRITE)
233                 ev.events |= EPOLLOUT;
234
235         return epoll_ctl(io_masterfd, EPOLL_CTL_ADD, fd, &ev) == 0;
236 }
237
238
239 static bool
240 io_event_change_epoll(int fd, short what)
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, EPOLL_CTL_MOD, 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);
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_add_kqueue(int fd, short what)
285 {
286         struct kevent kev;
287         short filter = 0;
288         unsigned int len = array_length(&io_evcache, sizeof kev);
289         bool ret;
290
291         if (what & IO_WANTREAD)
292                 filter = EVFILT_READ;
293
294         if (what & IO_WANTWRITE)
295                 filter |= EVFILT_WRITE;
296
297         if (len >= 100) {
298                 ret = io_event_kqueue_commit_cache();
299                 if (ret)
300                         array_trunc(&io_evcache);
301         }
302
303         EV_SET(&kev, fd, filter, EV_ADD | EV_ENABLE, 0, 0, NULL);
304         return array_catb(&io_evcache, (char*) &kev, sizeof (kev));
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);
315
316         if (!i)
317                 return false;
318         if (i->what == what)
319                 return true;
320 #ifdef DEBUG
321         Log(LOG_DEBUG, "io_event_add(): fd %d (arg: %d), what %d.", i->fd, fd, what);
322 #endif
323
324         i->what |= what;
325
326 #ifdef IO_USE_EPOLL
327         return io_event_change_epoll(fd, i->what);
328 #endif
329
330 #ifdef IO_USE_KQUEUE
331         return io_event_add_kqueue(fd, what);
332 #endif
333
334 #ifdef IO_USE_SELECT
335         if (fd > select_maxfd)
336                 select_maxfd = fd;
337
338         if (what & IO_WANTREAD)
339                 FD_SET(fd, &readers);
340         if (what & IO_WANTWRITE)
341                 FD_SET(fd, &writers);
342
343         return true;
344 #endif
345 }
346
347
348 bool
349 io_setnonblock(int fd)
350 {
351         int flags = fcntl(fd, F_GETFL);
352         if (flags == -1)
353                 return false;
354
355 #ifndef O_NONBLOCK
356 #define O_NONBLOCK O_NDELAY
357 #endif
358         flags |= O_NONBLOCK;
359
360         return fcntl(fd, F_SETFL, flags) == 0;
361 }
362
363
364 bool
365 io_close(int fd)
366 {
367         io_event *i = io_event_get(fd);
368         if (i) {
369                 memset(i, 0, sizeof(io_event));
370                 i->fd = -1;
371         }
372 #ifdef IO_USE_SELECT
373         FD_CLR(fd, &writers);
374         FD_CLR(fd, &readers);
375
376         if (fd == select_maxfd)
377                 select_maxfd--;
378 #endif
379 #ifdef IO_USE_KQUEUE
380         if (array_length(&io_evcache, sizeof (struct kevent)))  /* pending data in cache? */
381                 io_event_kqueue_commit_cache();
382 #endif
383         return close(fd) == 0;  /* both epoll an kqueue will remove fd from all sets automatically */
384 }
385
386
387 bool
388 io_event_del(int fd, short what)
389 {
390 #ifdef IO_USE_KQUEUE
391         struct kevent kev;
392         short filter = 0;
393 #endif
394         io_event *i = io_event_get(fd);
395 #ifdef DEBUG
396         Log(LOG_DEBUG, "io_event_del(): trying to delete eventtype %d on fd %d", what, fd);
397 #endif
398         assert(i);
399         if (!i)
400                 return false;
401
402         i->what &= ~what;
403
404 #ifdef IO_USE_EPOLL
405         return io_event_change_epoll(fd, i->what);
406 #endif
407
408 #ifdef IO_USE_KQUEUE
409         if (what & IO_WANTREAD)
410                 filter = EVFILT_READ;
411
412         if (what & IO_WANTWRITE)
413                 filter |= EVFILT_WRITE;
414
415         EV_SET(&kev, fd, filter, EV_DELETE, 0, 0, NULL);
416         return kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
417 #endif
418
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 kevents *newevents;
515         struct timespec ts;
516         int newevents_len;
517         short type;
518         ts.tv_sec = tv->tv_sec;
519         ts.tv_nsec = tv->tv_usec * 1000;
520         
521         do {
522                 newevents_len = array_length(&io_evcache, sizeof (struct kevent));
523                 newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
524                 assert(newevents_len >= 0);
525                 if (newevents_len < 0)
526                         newevents_len = 0;
527 #ifdef DEBUG
528                 if (newevents_len)
529                         assert(newevents);
530 #endif
531
532                 ret = kevent(io_masterfd, newevents, newevents_len, kev, 100, &ts);
533                 if ((newevents_len>0) && ret != -1)
534                         array_trunc(&io_evcache);
535
536                 total += ret;
537                 if (ret <= 0)
538                         return total;
539
540                 for (i = 0; i < ret; i++) {
541                         type = 0;
542                         if (kev[i].flags & EV_EOF)
543                                 type = IO_ERROR;
544
545                         if (kev[i].filter & EV_ERROR)
546                                 type = IO_ERROR;
547
548                         if (kev[i].filter & EVFILT_READ)
549                                 type |= IO_WANTREAD;
550
551                         if (kev[i].filter & EVFILT_WRITE)
552                                 type |= IO_WANTWRITE;
553
554                         io_docallback(kev[i].ident, type);
555                 }
556
557                 ts.tv_sec = 0;
558                 ts.tv_nsec = 0;
559
560         } while (ret == 100);
561
562         return total;
563 }
564 #endif
565
566
567 int
568 io_dispatch(struct timeval *tv)
569 {
570 #ifdef IO_USE_SELECT
571         return io_dispatch_select(tv);
572 #endif
573 #ifdef IO_USE_KQUEUE
574         return io_dispatch_kqueue(tv);
575 #endif
576 #ifdef IO_USE_EPOLL
577         return io_dispatch_epoll(tv);
578 #endif
579 }
580
581
582 /* call the callback function inside the struct matching fd */
583 static void
584 io_docallback(int fd, short what)
585 {
586         io_event *i;
587 #ifdef DEBUG
588         Log(LOG_DEBUG, "doing callback for fd %d, what %d", fd, what);
589 #endif
590         i = io_event_get(fd);
591         assert(i);
592
593         if (i->callback)        /* callback might be 0 if previous callback function called io_close on this fd */
594                 i->callback(fd, (what & IO_ERROR) ? i->what : what);
595         /* if error indicator is set, we return the event(s) the app asked for */
596 }